Using programmable keys in C#?

296 Views Asked by At

I am wanting to create a simple application that when running would wait for a specific key or combination of keys OR even button presses on a mouse. I am using the MouseKeyHook API in order to get this information, however I am running into a few issues.

This seems to work perfectly, as I can compare the string values of the Buttons with what I have saved. Now here is where the real issue begins. Whenever I press any programmable button on my keyboard I dont get ANY output? Why?

private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
    HotKey.Text = e.KeyChar.ToString();
}

private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
{
    HotKey.Text = e.Button.ToString();
}

When Hooking using the MouseKeyHook API, I was able to get the two mouse buttons XButton1 and XButton2 but I got nothing for my keyboard buttons.

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

Programmable keys on keyboard tend to use specific APIs that require vendor driver software. That software maps a key press to an action (or sequence of actions) that translate to standard operations. For example, press button X could correspond to pressing the A key five times in a row and your software should be able to pick up those 5 presses.

Mouse buttons are pretty generic and I believe the windows API allows for 8 standard buttons (see the API reference here). Any more than that requires specific vendor drivers again and for those buttons to be mapped.