DelphiFAQ Home Search:

Get Shift/ Ctrl/ Alt Key state

 

comments1 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

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.
 
You don't like the formatting? Check out SourceCoder then!

Comments:

2008-07-21, 02:28:40
anonymous from South Africa  
rating
Thanks...works well

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.