Feb 092013
 

Let’s assume very simple code for purpose of key detection:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    Debug.WriteLine(keyData);
}

Whichever key you press you will get short output in Output window. Totally boring.

But interesting thing happens when you press Alt key. Output is (sort of) as expected:

Menu, Alt

However, if you put breakpoint there and hover your mouse over keyData you will see that value is actually Keys.RButton | Keys.ShiftKey | Keys.Alt. Which one is it?

Answer is that it does not really matter. If we check values in Keys enumeration we will find following:

RButton  = 2
ShiftKey = 16
Menu     = 18
Alt      = 262144

We can see or as simple addition and thus Menu + Alt will give you 262162. Same result can also be obtained with RButton + ShiftKey + Alt.

CLR will get just integer with value of 0x40000 (262144 in decimal) from Windows. No matter what you do, it stays in integer domain. When you say Keys.Menu you are just saying 18 in human readable way. So for any text display code actually needs to guess what combination will give that result.

Code that handles string conversion goes from highest element and matches Alt and Menu. Code that handles tooltips matches values from lower end and thus it will find RButton, ShiftKey and Alt. Computer cannot say that one solution is better than other.

Regardless which combination you choose result will be the same.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>