petershaw8
asked on
Caps Lock key and Num Lock key
If I press Caps Lock key or Num Lock key
I wish the status bar shows 'Cap' or 'Num'. How can I do this?
I wish the status bar shows 'Cap' or 'Num'. How can I do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
btw: i could send ya a statusbar component doing just this. post ur email addr; i'll send it ovuh 2 ya.
BlackDeath.
BlackDeath.
You really have to poll because caps lock could be changed when another app has the focus. Your app would not get a keydown event.
BTW, here is how you can set and set the NUMLOCK state. (This is Microsoft's method. It is a translation of C code from the MSDN).
function GetNumLock: Boolean;
var KeyState: Integer;
begin
KeyState := GetKeyState(VK_NUMLOCK);
Result := KeyState and 1 = 1;
end;
procedure SetNumLock(bState: Boolean);
var KeyState: TKeyBoardState;
begin
GetKeyboardState(KeyState) ;
if (bState and (KeyState[VK_NUMLOCK] and 1 = 0)) or
((not bState) and (KeyState[VK_NUMLOCK] and 1 = 1)) then
begin
// Simulate a key press
keybd_event( VK_NUMLOCK,
$45,
KEYEVENTF_EXTENDEDKEY or 0,
0 );
// Simulate a key release
keybd_event( VK_NUMLOCK,
$45,
KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,
0);
end;
end;
Cheers,
Phil.
BTW, here is how you can set and set the NUMLOCK state. (This is Microsoft's method. It is a translation of C code from the MSDN).
function GetNumLock: Boolean;
var KeyState: Integer;
begin
KeyState := GetKeyState(VK_NUMLOCK);
Result := KeyState and 1 = 1;
end;
procedure SetNumLock(bState: Boolean);
var KeyState: TKeyBoardState;
begin
GetKeyboardState(KeyState)
if (bState and (KeyState[VK_NUMLOCK] and 1 = 0)) or
((not bState) and (KeyState[VK_NUMLOCK] and 1 = 1)) then
begin
// Simulate a key press
keybd_event( VK_NUMLOCK,
$45,
KEYEVENTF_EXTENDEDKEY or 0,
0 );
// Simulate a key release
keybd_event( VK_NUMLOCK,
$45,
KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,
0);
end;
end;
Cheers,
Phil.
ASKER
Thanks all of you.
My email address is exex8@hotmail.com.
It is very nice if Blackdeath send me your status bar. BTW, Blackdeath: what message handling proc should be?
My email address is exex8@hotmail.com.
It is very nice if Blackdeath send me your status bar. BTW, Blackdeath: what message handling proc should be?
u can hook a message handling procedure in2 the message chain 4 keyboard events. there u'll get 2 parameters containing all u need bout the pressed key. i've got a sample @home. mayb i remember 2 bring it w/ me 2morrow so i can send it ovuh. it's 2 much trouble here @work 2 gather all i've needed the last time i wrote it. it's not so much the proc itself; it's the parameter evaluation.
the statusbar is on the way...
;-)
BlackDeath.
the statusbar is on the way...
;-)
BlackDeath.
if (GetKeyState(VK_CAPITAL) and $01) <> 0 then
// caps active
else
// caps inactive
u can poll this in a timer event or put it in the keydown event of the form or in a message handling proc.
more?