在Delphi中的OnKeyDown问题 [英] Problem with OnKeyDown in Delphi

查看:345
本文介绍了在Delphi中的OnKeyDown问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Delphi合作.我想跟踪按下哪个键.我正在使用TForm的KeyDown事件.它工作正常,但问题是,如果我按小写字母,尽管它使我该字母大写.如何识别所按下的键是小写还是大写?

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?

推荐答案

如果要跟踪字母数字键,则应使用KeyPress.试试这个:

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;

KeyDown的问题是响应于按键被按下,如果要在键盘上输入"K"或"k",您肯定按下了相同的按钮,对吗?因此,如果要坚持使用KeyDown,则需要单独检查Caps Lock键是否处于打开状态或Shift键是否处于按下状态.要测试是否打开了切换键(例如Caps Lock),或者是否按下了常规键,您可以使用

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;

要检查Caps Lock键是否打开,请使用IsKeyOn(VK_CAPITAL).要检查是否按下了Shift键,请使用IsKeyDown(VK_SHIFT).

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).

另一种检查是否按下Shift键的方法(仅在OnKeyDown事件处理程序中有效)是检查ssShift in Shift,其中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.

(顺便说一句,因为Caps Lock处于打开状态,这是通过Shift键来抵消的(也就是说,如果在Caps Lock处于打开状态时按Shift + A,则 small "a"插入),则在进行资本测试时要采用的支票是

(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)

使用xor运算符.)

这篇关于在Delphi中的OnKeyDown问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆