Delphi Tips
Get Shift Ctrl Alt Key State
This code determines the state of
shift, alt and ctrl keys. You can
do this while your application is
initializing, in order to do
something or not to do something.
Keywords: keystate, keyboard state
Note: Thanks to Mary Atkins-Shington for reporting this
GetKeyState and GetAsyncKeyState only work with Win95,
Win98, NT4, Terminal Server and Windows 2000. But on
WinME it always returns zero. These two functions are
simply skipped by Millenium Edition!
Official explanation from Microsoft:
Intentionally disabled.
It didn't work all that well on some newer hardware,
and worked less well with the passage of time, so
it was fully disabled in ME.
//Tests whether the high order bit of the given
//word is set.
function HighOrderBitSet (theWord: Word): Boolean;
const
HighOrderBit = 15;
type
BitSet = set of 0..15;
begin
HighOrderBitSet := (HighOrderBit in BitSet(theWord));
end;
..
begin
..
AltKeyDown := HighOrderBitSet(Word(GetKeyState(
VK_MENU)));
CtrlKeyDown := HighOrderBitSet(Word(GetKeyState(
VK_CONTROL)));
ShiftKeyDown := HighOrderBitSet(Word(GetKeyState(
VK_SHIFT)));
LeftShiftKeyDown := HighOrderBitSet(Word(GetKeyState(
VK_LSHIFT)));
// other VK's:
// VK_LSHIFT VK_RSHIFT
// VK_LCONTROL VK_RCONTROL
// VK_LMENU VK_RMENU
end.
Back to Index of Tips |