如何模拟按键C#?

时间:2019-06-15 14:57:38

标签: c#

我想制作一个可以通过按PageUP在csgo中执行命令的程序,但是在其他程序中它不起作用

我已经尝试过

SendKeys.SendWait("{PGUP}");
SendKeys.Send("{PGUP}");

现在将我的帖子标记为我尝试过的重复

        [DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
        public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
        public const int VK_RCONTROL = 0xA3; //Right Control key code
        public const int VK_PRIOR = 0x21; //Right Control key code


keybd_event(VK_PRIOR, 0, KEYEVENTF_EXTENDEDKEY, 
keybd_event(VK_PRIOR, 0, KEYEVENTF_KEYUP, 0);

这是我正在使用的简化代码,仅包含此问题所需的部分

public partial class Form2 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
        public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
        public const int VK_RCONTROL = 0xA3; //Right Control key code
        public const int VK_PRIOR = 0x21; //Right Control key code
        //keycodes 
        //https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes

        //Register Hotkey IDs
        const int MYACTION_HOTKEY_ID = 1;
        const int MYACTION_HOTKEY_ID_2 = 2;


        private void Form2_Load(object sender, EventArgs e)
        {
       // Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
            // Compute the addition of each combination of the keys you want to be pressed
            // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6...
            RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int)Keys.F9);
            RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID_2, 0, (int)Keys.F11);


       private void Csgo_cmd()
        {
            //input page up
            //SendKeys.SendWait("{PGUP}");
            //InputSimulator.
            keybd_event(VK_PRIOR, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(VK_PRIOR, 0, KEYEVENTF_KEYUP, 0);


//send key if strg+shift+F9 is pressed 
protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID)
                {
                    // My hotkey has been typed
                    richTextBox1.AppendText("pressed");
                    Csgo_cmd();
                    // Do what you want here         
                    // ...
                }
                base.WndProc(ref m);

        }

0 个答案:

没有答案