如何按住键C#

时间:2014-03-25 12:55:26

标签: c# user32

我想在这个控制台程序中完成的是通过使用user32.dll来按住一个键。我知道我没有发送扩展密钥。但我不认为将其作为扫描码发送也是正确的。而且我认为我正在传递正确的旗帜来抓住钥匙..我也知道我必须做一把钥匙。但截至目前,我所需要的只是将按键推下来。任何帮助将不胜感激,因为现在下面的代码不起作用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace pressand_hold
{
class Program
{
    [DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

    const int VK_UP = 0x26; //up key
    const int VK_DOWN = 0x28;  //down key
    const int VK_LEFT = 0x25;
    const int VK_RIGHT = 0x27;
    const uint KEYEVENTF_KEYUP = 0x0002;
    const uint SCANCODE = 0x0008;
    const int KEY_0 = 11;

    internal enum ScanCodeShort : short
    {

        KEY_9 = 10,
        KEY_A = 30,
        KEY_B = 48,
        KEY_C = 46,
        KEY_D = 32,
        KEY_E = 18,
        KEY_F = 33,
        KEY_G = 34,
        KEY_H = 35,
        KEY_I = 23,
        KEY_J = 36,
        KEY_K = 37,
        KEY_L = 38,
        KEY_M = 50,
        KEY_N = 49,
        KEY_O = 24,
        KEY_P = 25,
        KEY_Q = 16,
        KEY_R = 19,
        KEY_S = 31,
        KEY_T = 20,
        KEY_U = 22,
        KEY_V = 47,
        KEY_W = 17,
        KEY_X = 45,
        KEY_Y = 21,
        KEY_Z = 44,
    }

    static void Main(string[] args)
    {
        Thread.Sleep(2000);
        // push V key
       keybd_event((byte)ScanCodeShort.KEY_V, 0x45, 0, 0);
      // release V key
       keybd_event((byte)ScanCodeShort.KEY_V, 0x45, KEYEVENTF_KEYUP, 0);
       Console.WriteLine("done");          
   Console.Read();
    }
}
}

1 个答案:

答案 0 :(得分:2)

keybd_event的第二和第三个参数是错误的。

查看pinvoke definitionmsdn

第二个参数应为0x45

第三个参数不能为8.按键必须为0。

可能是这样的:

static void Main(string[] args)
{
    Thread.Sleep(2000);
    // push V key
    keybd_event((byte)ScanCodeShort.KEY_V, 0x45, 0, 0);

    Console.WriteLine("done");
    Console.Read();
}

ScanCode不是String的可视化表示形式(数字或字母数字)。

小心你的代码。 47是0x2F en hexa,它是Virtual Key Codes

中的VK_HELP
KEY_V = 86,

*完整代码*

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace pressand_hold
{
    internal class Program
    {
        internal enum ScanCodeShort : short
        {
            KEY_0 = 48,
            KEY_1,
            KEY_2,
            KEY_3,
            KEY_4,
            KEY_5,
            KEY_6,
            KEY_7,
            KEY_8,
            KEY_9,
            KEY_A = 65,
            KEY_B,
            KEY_C,
            KEY_D,
            KEY_E,
            KEY_F,
            KEY_G,
            KEY_H,
            KEY_I,
            KEY_J,
            KEY_K,
            KEY_L,
            KEY_M,
            KEY_N,
            KEY_O,
            KEY_P,
            KEY_Q,
            KEY_R,
            KEY_S,
            KEY_T,
            KEY_U,
            KEY_V,
            KEY_W,
            KEY_X,
            KEY_Y,
            KEY_Z,
        }

        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        private static void Main(string[] args)
        {
            Thread.Sleep(2000);

            keybd_event((byte)ScanCodeShort.KEY_V, 0x45, 0, 0);

            Console.WriteLine("done");
            Console.Read();
        }
    }
}

要按住某个键,请使用循环(while()for()等...)