Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent Alt codes in a .NET app?

Tags:

.net

mnemonics

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!

like image 800
AndrewL Avatar asked Oct 14 '22 21:10

AndrewL


2 Answers

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();
}
like image 58
AndrewL Avatar answered Oct 18 '22 01:10

AndrewL


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!

like image 24
Vercas Avatar answered Oct 18 '22 01:10

Vercas