views:

151

answers:

1

Hello friends,

I am working with Delphi. I want to track on which key is pressed. I am using KeyDown event of TForm. It is working fine but the problem is that, if I press and lower case letter, though it gives me upper case of that letter. How can I recognize the key pressed is lower case or upper case?

+9  A: 

If you want to track alphanumerical keys, then you should use KeyPress. Try this:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  ShowMessage(Key);
end;

The problem with KeyDown is that is responds to a key being depressed, and surely enough, if you want to enter either "K" or "k" on the keyboard, you press the same button, right? So if you want to stick to KeyDown, then you need to check separately if the Caps Lock key is on, or if the Shift key is depressed. To test if a toggle key (such as Caps Lock) is on, or if a regular key is depressed, you can use

function IsKeyDown(const VK: integer): boolean;
begin
  IsKeyDown := GetKeyState(VK) and $8000 <> 0;
end;

function IsKeyOn(const VK: Integer): boolean;
begin
  IsKeyOn := GetKeyState(VK) and 1 = 1;
end;

To check if the Caps Lock key is on, use IsKeyOn(VK_CAPITAL). To check if the shift key is depressed, use IsKeyDown(VK_SHIFT).

An alternative way of checking if the shift key is depressed, which only works in the OnKeyDown event handler, is to check if ssShift in Shift, where Shift is a parameter of that event handler function.

(By the way, because the action of Caps Lock being on is counteracted by the Shift key (that is, if you press Shift+A when Caps Lock is on, a small "a" is inserted), the check to employ when testing for capitals is

IsKeyOn(VK_CAPITAL) xor IsKeyDown(VK_SHIFT)

using the xor operator.)

Andreas Rejbrand
Ok, thanks for this code, can you tell me what is the meaning of `$8000 <> 0` and `1=1` in both the function?
Himadri
`GetKeyState` returns a cardinal in which the bits are flags, so by anding I clear all bits but the interesting one, and then I check if the result is zero or not. That is, `GetKeyState(VK) and $8000 <> 0` should be read `(GetKeyState(VK) and $8000) <> 0` where 8000 in hex is 1000000000000000 in binary. It is all well-documented: http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx
Andreas Rejbrand
yes I already read it but I didn't get the return value. What is low-order bit, high-order bit?
Himadri
GetKeyState returns a 16-bit signed number. Internally, this is represented as a string of 16 binary digits ("bits", ones and zeroes). For instance, 0000010100001011 represents the number 1291. The high-order bit is the first bit (0 in this example), and the last order bit is the last bit (1 in this example). So the return value of GetKeyState should not be interpreted as a number (as 1291) in binary, but rather as a string of flags, either on (1) or off (0). So if the function returns 1000000000001001 or something else where the first bit is 1, then the key is down.
Andreas Rejbrand
Oh, that's why you check different things in both the conditions. Thanks for the reply and also for clearing my doubts. + 1.
Himadri