3

I want to write simple program that send my user and password in some places for example when I want to log in to website and i found this project that listening to keyboard.

SO i have this function:

private void HookManager_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{

}

private void HookManager_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{

}

private void HookManager_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

}

And when i press key for example a i have this pice of result:

int value = e.KeyValue; // 65
Keys key = e.KeyCode;   // A

So i wondoer how to catch specific keyboard combination and not only one key for example Ctrl + l ?

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
user979033
  • 5,430
  • 7
  • 32
  • 50
  • 1
    The [RegisterHotKey](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerhotkey) function could be more useful here, I think. Also, easier to handle. See this answer (and the Docs) for an example: [Capture a keyboard keypress in the background](https://stackoverflow.com/questions/15413172/capture-a-keyboard-keypress-in-the-background?answertab=active#tab-top). If you use it, don't forget to call [UnregisterHotKey](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-unregisterhotkey). – Jimi Dec 08 '18 at 13:21

2 Answers2

5

You can use code like this for KeyDown:

if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
}

Based on the KeyEventArgs documentation from MSDN:

A KeyEventArgs, which specifies the key the user pressed and whether any modifier keys (CTRL, ALT, and SHIFT) were pressed at the same time, is passed with each KeyDown or KeyUp event.

The KeyDown event occurs when the user presses any key. The KeyUp event occurs when the user releases the key. Duplicate KeyDown events occur each time the key repeats, if the key is held down, but only one KeyUp event is generated when the user releases the key.

The KeyPress event also occurs when a key is pressed. A KeyPressEventArgs is passed with each KeyPress event, and specifies the character that was composed as a result of each key press.

nemanja228
  • 556
  • 2
  • 16
  • under whan function ? (KeyUp\KeyDown\KeyPress) – user979033 Dec 08 '18 at 06:05
  • This one under KeyDown specifically – nemanja228 Dec 08 '18 at 06:06
  • As for Ctrl + L, you would have: `if (e.Control && e.KeyCode == Keys.L)` – nemanja228 Dec 08 '18 at 06:07
  • This is not working, i also try it with another latter and same result, it semms that e.Alt \ e.Control \ e.Shift allways false – user979033 Dec 08 '18 at 06:08
  • I've checked now the project that you're using and it seems that it's based on Win32 API, and it clearly mentions that for multiple keys you need to track down which keys were released (KeyUp). The code for my answer works for WinForm controls in general, but probably not for this. If you can describe your case better, maybe we can help you. – nemanja228 Dec 08 '18 at 11:46
1

Overriding the ProcessDialogKey() method is the generic solution:

protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.I))
        {
            MessageBox.Show("Ctrl+I");
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }
Anas Alweish
  • 2,818
  • 4
  • 30
  • 44