Question

What I am trying to accomplish in this console program is to be able to press and hold a key, by using user32.dll. I know I am not sending an extended key. But I dont think sending it as a scancode is right either. And I think I am passing it the right flag to just hold the key.. I also know I will have to do a key up. but as of right now all I need is to get the key pushed down. Any help would be much appreciated, as of right now the code below does not work

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

    const int VK_UP = 0x26, VK_DOWN = 0x28, VK_LEFT = 0x25, VK_RIGHT = 0x27;
    const uint KEYEVENTF_KEYUP = 0x0002, 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, }

    private 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();
    }
}
Était-ce utile?

La solution

2nd and 3rd arguments of keybd_event are wrong.

Take a look to pinvoke definition and msdn

2nd argument should be 0x45

3rd argument cannot be 8. Must be 0 to push key.

May be something like :

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();
}

A ScanCode is nothing else that a visual representation of a String (numbers or alphanumerics).

Be careful with your code. 47 is 0x2F en hexa, and it is VK_HELP in Virtual Key Codes

KEY_V = 86,

* complete code *

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();
        }
    }
}

To hold a key, use a loop (while(), for(), etc...)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top