tags:

views:

47

answers:

3

Hi,

I have a .NET app with buttons which use numbers for mnemonics. (e.g. "&1 - Check" and "&2 - Cash") When I hit Alt+1, I expect the Check button to be clicked. This works when I use the standard number keys on a keyboard. However, when I hit Alt+1 using the number pad, Windows takes over and inserts the symbol that matches the "alt code" of 1. See http://alt-codes.org/list/. How can I prevent this from happening in my application?

Thanks!

+1  A: 

This Alt+numkey combination is a Windows feature (very useful), you cannot bypass it.
I have tried this a while ago while working on a game (you know... skills bar shortcuts? ☺) and I have done some research... It is impossible.
I'd be very happy to see a way over this, too!

Vercas
Holy !@#$ (I cannot comment on another answers), I am glad you found a way! :O Still, I think it is a bug. ☺ Edit: Keyboard hooks didn't even run for me...
Vercas
+1  A: 

A possible way to do this would be to use keyboard hooks, intercept the keypresses, and then act as you see fit. Now, will it solve the above problem, and stop the OS from inserting the charcode, I dont know.

jasper
+1  A: 

This can be done by enabling KeyPreview on the form and handling the KeyUp event. The following code bypasses the Windows alt codes functionality and causes the button to click when I hit Alt+NumPad1:

if (e.Alt && e.KeyCode == Keys.NumPad1)
{
     e.Handled = true;
     button1.PerformClick();
}
AndyGeek