The key you are looking for, S, has code $53
. The key you specified has code 53
and is the number 5. The difference is the $
which signifies hexadecimal.
You'd avoid such silly mistakes, and make the code much clearer, if you let the compiler do the work:
Key = ord('S')
You really don't want to use magic constants in your program. That is very important.
Note that Key
is a virtual key code and the convention is that for the 26 keys of the Latin alphabet, they are represented by the ordinal value of the uppercase letter.
The message already passes the state of the modifier keys in the Shift
argument, so it is idiomatic to write the test as follows:
if (ssCtrl in Shift) and (Key = ord('S')) then
Your test using GetKeyState
does work well, but it's just not idiomatic.
Note that this test, which matches that in the question will, ignores the state of the other modifier keys. Indeed, the C# code in the question also ignores the state of the ALT modifier.
So you may want a true test for CTRL + S you must also check that the other modifiers are up:
if ([ssCtrl] = Shift*[ssCtrl, ssShift, ssAlt]) and (Key = ord('S')) then
All this said, it's usually much easier to manage your shortcuts using actions. This will allow you to specify shortcuts directly, and let the framework detect the low level key events that make up a shortcut. What's more actions allow you to centralise handling of the actions behind buttons and menus without you repeating yourself.