Автонажатия клавиш - C#

Узнай цену своей работы

Формулировка задачи:

Нужно что бы при клике на баттон происходило нажатия клавиш Ctrl+S. Нашел код:
 
        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
 
        public Form1()
        {
                  RegisterHotKey(this.Handle, 0, 0, (int)Keys.D1);
        }
        
        protected override void WndProc(ref Message m)
        {
 
            if (m.Msg == 0x0312)
            {
                if (m.WParam.ToInt32() == 0)
                {
                    MessageBox.Show("Нажата клавиша 1");
                }
            }

            base.WndProc(ref m);
        }
Он отслеживает если нажата клавиша 1 на клавиатуре выводит месседжбокс. А как сделать, чтобы оно само нажимало комбинацию клавиш? Помогите кодом пожалуйста.

Решение задачи: «Автонажатия клавиш»

textual
Листинг программы
  
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
 
        IntPtr chromeHandle = FindWindow(null, "Chrome");
 
            if (chromeHandle == IntPtr.Zero)
    {
        MessageBox.Show("Calculator is not running.");
        return;
    }
            SetForegroundWindow(chromeHandle);
                SendKeys.Send("Space"); 
   
        }

Оцени полезность:

5   голосов , оценка 4.2 из 5
Похожие ответы