0

I would like to keep a key pressed while my program runs, so I did this:

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

    const int KEY_DOWN_EVENT = 0x0001; //Key down flag
    const int KEY_UP_EVENT = 0x0002; //Key up flag

    byte VK_UP = 0x26;

    public Form1()
    {
        InitializeComponent();

        keybd_event(VK_UP, 0, KEY_DOWN_EVENT, 0);
    }

    void gkh_KeyDown(object sender, KeyEventArgs e)
    {
        Debug.WriteLine(e.KeyCode.ToString()); //it only executes once
    }

but it only press the key once. What am I missing?


Can't believe it's impossible on C#!! even delphi can do it !!


What I really want to do is that:

Assuming I press the key 'a' and some seconds later I press the key 'b'. When I loose the key 'b' I want that 'a' continues to be displayed on screen.

MuriloKunze
  • 15,195
  • 17
  • 54
  • 82
  • 3
    If you press and hold a key down, how many times does the key go down? If your answer is more than 1, then you're thinking about the problem incorrectly. – spender Apr 30 '13 at 16:48
  • `KeyDown` only executes when you press the key down. You probably want some sort of recurring timer event that executes as long as the key is down. you can use `KeyUp` to chedck – Sam I am says Reinstate Monica Apr 30 '13 at 16:49
  • You could stick the key down with some Blu Tack... ;) – Matthew Watson Apr 30 '13 at 16:50
  • @spender it press twice.. too weird. – MuriloKunze Apr 30 '13 at 17:07
  • I want to ask you a clear question,do you want to press down key 2 times during your page load? – ridoy Apr 30 '13 at 17:14
  • @ridoy I want it keeps pressed as if I was holding the button. So the keydown method must be executing all the time. – MuriloKunze Apr 30 '13 at 17:18
  • 1
    @MuriloKunze ... my point is that the keydown method should execute only once, because that's the number of times that the key goes down when you hold it. It won't execute "all the time" because that's not what the keydown event represents. To be clear, the keydown event is dispatched as the key moves from a keyup position to a keydown position. – spender Apr 30 '13 at 17:23
  • 1
    Not sure whether it is possible,because any key press event occurs only once,not for a long time.Possible solution will be keeping that key event inside a loop or keep program wait until other key is pressed. – ridoy Apr 30 '13 at 17:26

3 Answers3

1

the KeyDown method fires only once when you press the key down. If I were you, I'd make it look like

void gkh_KeyDown(object sender, KeyEventArgs e)
{
    //represent that the key is down
    KeysDown[e.KeyCode] = true; // you may represent that the key is down however you want
}

and then make a KeyUp event

void gkh_KeyUp(object sender, KeyEventArgs e)
{
    //represent that the key is not down
    KeysDown[e.KeyCode] = false; // you may represent that the key is down however you want
    Debug.WriteLine(e.KeyCode.ToString()); //it only executes once
}

and then, I'd have some sort of periodic event

void PeriodicEvent(object sender, KeyEventArgs e)
{
     // if the key is down, write the key.
     if (KeysDown[e.KeyCode])
         Debug.WriteLine(e.KeyCode.ToString());
}
1

I think you are hoping for keyboard auto-repeat. That is however a function of the keyboard controller, the chip built into your keyboard. Windows doesn't do anything, it can only tell the keyboard controller about the desired delay and repeat rate, as configured in the Control Panel + Keyboard applet.

So your keybd_event() will never produce more than one keystroke. You can fix it with a Timer.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

The key is still technically down, you just see only one event in your form as already explained by others.

You can verify this by using the GetKeyState() API to check the state of your desired key. Use the example posted by @parsely72 here

Community
  • 1
  • 1
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40