6

I just discovered that the caps lock and shift key (and probably some more of the keys) affect all of the keyboards connected to the computer. (one of the hazards of testing a program that I coded, I only have two hands so it took me ages to realize a problem like this) So separating the keystrokes is fine, but the shift/caps locks drives everything crazy (When one user shifts to capitalize, all users' input at that moment are capitalized as well)

Can I capture Capslock and Shift keypress in FormKeyPress? Anywhere else for that matter?
Can I save a Shift keypress? (so I can properly apply the 'shift' to the respective user input)

Any other suggestion to solve this problem is welcome as well.

3 Answers 3

10

to check if shift ley is down try this:

if GetKeyState(VK_SHIFT)<0 //tests if shiftkey is down
 then ShowMessage('shift key is down'');

and to detect if caps lock is on try this:

if Odd(GetKeyState(VK_CAPITAL)) //tests if caps lock is on
then showmessage('caps lock is on');

and if you want to check status of both shift and capslock :

      if Odd(GetKeyState(VK_CAPITAL)) then 
            if GetKeyState(VK_SHIFT)<0 then 
            showmessage('capslock is on and shift key is down too')
           else
          showmessage('capslock is on but shift key is NOT down');

getkeystate is a windows api function you can read more about it here

2

You can capture them in the OnKeyDown event

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_SHIFT then
    ShowMessage('Shift Pressed');

  if Key = VK_CAPITAL then
    ShowMessage('Caps Lock Pressed');
end;
2
if Key = VK_SHIFT then
  ShowMessage('Shift Pressed');

there should have been:

if shift = [ssShift] then ... 

The program tests Shift: TShiftState for SHIFT key hold down, not key :word; it is checked for other ordinal characters

It doesn't work for CAPSLOCK - I couldn't find the code. There are only: ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble, so it seems have to use in this case:

if Odd(GetKeyState(VK_CAPITAL)) then ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.