delphi 键盘按键模拟
Keyboard events, along with mouse events, are the primary elements of a user's interaction with your program.
键盘事件和鼠标事件是用户与程序交互的主要元素。
Below is information on three events that let you capture a user's keystrokes in a Delphi application: OnKeyDown, OnKeyUp and OnKeyPress.
以下是有关三个事件的信息,这些事件使您可以在Delphi应用程序中捕获用户的击键: OnKeyDown , OnKeyUp和OnKeyPress 。
向下,向上,按,向下,向上,按... ( Down, Up, Press, Down, Up, Press... )
Delphi applications can use two methods for receiving the input from the keyboard. If a user has to type something in an application, the easiest way to receive that input is to use one of the controls that automatically responds to keypresses, such as Edit.
Delphi应用程序可以使用两种方法从键盘接收输入。 如果用户必须在应用程序中键入内容,则接收该输入的最简单方法是使用自动响应按键的控件之一,例如“编辑”。
At other times and for more general purposes, however, we can create procedures in a form that handle three events recognized by forms and by any component that accepts keyboard input. We can write event handlers for these events to respond to any key or key combination the user might press at runtime.
但是,在其他时候和出于更一般的目的,我们可以以某种形式创建过程,以处理由表单以及接受键盘输入的任何组件识别的三个事件。 我们可以为这些事件编写事件处理程序,以响应用户在运行时可能按下的任何键或组合键。
Here are those events:
这些事件如下:
OnKeyDown - called when any key on the keyboard is pressedOnKeyUp - called when any key on the keyboard is releasedOnKeyPress - called when a key corresponding to an ASCII character is pressed
OnKeyDown-在按下键盘上的任何键时调用OnKeyUp-在释放键盘上的任何键时调用OnKeyPress-在按下与ASCII字符对应的键时调用
键盘处理器 ( Keyboard Handlers )
All the keyboard events have one parameter in common. The Key parameter is the key on the keyboard and is used to pass by reference of the value of the pressed key. The Shift parameter (in the OnKeyDown and OnKeyUp procedures) indicates whether the Shift, Alt, or Ctrl keys are combined with the keystroke.
所有键盘事件都有一个共同的参数 。 Key参数是键盘上的键,用于通过参考按下的键的值来传递。 Shift参数(在OnKeyDown和OnKeyUp过程中)指示Shift,Alt或Ctrl键是否与按键组合。
The Sender parameter references the control that was used to call the method.
Sender参数引用用于调用该方法的控件。
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState) ;
...
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState) ;
...
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char) ;
Responding when the user presses shortcut or accelerator keys, such as those provided with menu commands, does not require writing event handlers.
用户按下快捷键或加速键(例如菜单命令附带的快捷键)时做出响应,不需要编写事件处理程序。
什么是焦点? ( What Is Focus? )
Focus is the ability to receive user input through the mouse or keyboard. Only the object that has the focus can receive a keyboard event. Also, only one component per form can be active, or have the focus, in a running application at any given time.
焦点是通过鼠标或键盘接收用户输入的能力。 只有具有焦点的对象才能接收键盘事件。 此外,在任何给定时间,正在运行的应用程序中,每种形式只有一个组件可以处于活动状态或具有焦点。
Some components, such as TImage, TPaintBox, TPanel and TLabel cannot receive focus. In general, components derived from TGraphicControl are unable to receive focus. Additionally, components that are invisible at run time (TTimer) cannot receive focus.
TImage , TPaintBox , TPanel和TLabel等某些组件无法获得焦点。 通常,从TGraphicControl派生的组件无法获得焦点。 此外,在运行时( TTimer )不可见的组件无法获得焦点。
OnKeyDown,OnKeyUp ( OnKeyDown, OnKeyUp )
The OnKeyDown and OnKeyUp events provide the lowest level of keyboard response. Both OnKeyDown and OnKeyUp handlers can respond to all keyboard keys, including function keys and keys combined with the Shift, Alt, and Ctrl keys.
OnKeyDown和OnKeyUp事件提供了最低级别的键盘响应。 OnKeyDown和OnKeyUp处理程序都可以响应所有键盘键,包括功能键以及与Shift , Alt和Ctrl键组合的键。
The keyboard events are not mutually exclusive. When the user presses a key, both the OnKeyDown and OnKeyPress events are generated, and when the user releases the key, the OnKeyUp event is generated. When the user presses one of the keys that OnKeyPress does not detect, only the OnKeyDown event occurs, followed by the OnKeyUp event.
键盘事件不是互斥的。 当用户按下一个键时,将同时生成OnKeyDown和OnKeyPress事件,并且当用户释放该键时,将生成OnKeyUp事件。 当用户按下OnKeyPress未检测到的键之一时,只会发生OnKeyDown事件,然后是OnKeyUp事件。
If you hold down a key, the OnKeyUp event occurs after all the OnKeyDown and OnKeyPress events have occurred.
如果按住某个键,则在所有OnKeyDown和OnKeyPress事件都发生之后,就会发生OnKeyUp事件。
OnKeyPress ( OnKeyPress )
OnKeyPress returns a different ASCII character for 'g' and 'G,' but OnKeyDown and OnKeyUp do not make a distinction between uppercase and lowercase alpha keys.
OnKeyPress返回用于'g'和'G'的不同ASCII字符,但是OnKeyDown和OnKeyUp不能区分大写和小写字母键。
键和班次参数 ( Key and Shift Parameters )
Since the Key parameter is passed by reference, the event handler can change Key so that the application sees a different key as being involved in the event. This is a way to limit the kinds of characters that the user can input, like to prevent users from typing alpha keys.
由于Key参数是通过引用传递的,因此事件处理程序可以更改Key,以便应用程序将不同的键视为事件所涉及。 这是一种限制用户可以输入的字符种类的方法,例如防止用户键入字母键。
if Key in ['a'..'z'] + ['A'..'Z'] then Key := #0
The above statement checks whether the Key parameter is in the union of two sets: lowercase characters (i.e. a through z) and uppercase characters (A-Z). If so, the statement assigns the character value of zero to Key to prevent any input into the Edit component, for example, when it receives the modified key.
上面的语句检查Key参数是否处于两个集合的组合中:小写字符(即a到z )和大写字符( AZ )。 如果是这样,则该语句将零的字符值分配给Key,以防止任何输入到Edit组件的操作,例如,当它收到修改后的key时。
For non-alphanumeric keys, WinAPI virtual key codes can be used to determine the key pressed. Windows defines special constants for each key the user can press. For example, VK_RIGHT is the virtual key code for the Right Arrow key.
对于非字母数字键,可以使用WinAPI 虚拟键代码来确定按下的键。 Windows为用户可以按的每个键定义特殊的常数。 例如, VK_RIGHT是右箭头键的虚拟键代码。
To get the key state of some special keys like TAB or PageUp, we can use the GetKeyState Windows API call. The key status specifies whether the key is up, down, or toggled (on or off - alternating each time the key is pressed).
要获取某些特殊键(如TAB或PageUp)的键状态,我们可以使用GetKeyState Windows API调用。 键状态指定键是向上,向下还是切换(打开或关闭-每次按下键时交替)。
if HiWord(GetKeyState(vk_PageUp)) <> 0 then
ShowMessage('PageUp - DOWN')
else
ShowMessage('PageUp - UP') ;
In the OnKeyDown and OnKeyUp events, Key is an unsigned Word value that represents a Windows virtual key. In order to get the character value from Key, we use the Chr function. In the OnKeyPress event, Key is a Char value that represents an ASCII character.
在OnKeyDown和OnKeyUp事件中, Key是代表Windows虚拟密钥的无符号Word值。 为了从Key获得字符值, 我们使用Chr函数。 在OnKeyPress事件中, Key是一个表示ASCII字符的Char值。
Both OnKeyDown and OnKeyUp events use the Shift parameter, of type TShiftState, a set flags to determine the state of the Alt, Ctrl, and Shift keys when a key is pressed.
OnKeyDown和OnKeyUp事件均使用TShiftState类型的Shift参数,该设置参数用于确定按下某个键时Alt,Ctrl和Shift键的状态。
For example, when you press Ctrl + A, the following key events are generated:
例如,当您按Ctrl + A时,将生成以下按键事件:
KeyDown (Ctrl)// ssCtrl
KeyDown (Ctrl+A) //ssCtrl + 'A'
KeyPress (A)
KeyUp (Ctrl+A)
将键盘事件重定向到窗体 ( Redirecting Keyboard Events to The Form )
To trap keystrokes at the form level instead of passing them to the form's components, set the form's KeyPreview property to True (using the Object Inspector). The component still sees the event, but the form has an opportunity to handle it first - to allow or disallow some keys to be pressed, for example.
要在表单级别捕获击键,而不是将其传递给表单的组件,请将表单的KeyPreview属性设置为True(使用Object Inspector )。 组件仍然可以看到事件,但是表单有机会首先处理它-例如,允许或不允许按下某些键。
Suppose you have several Edit components on a form and the Form.OnKeyPress procedure looks like:
假设您在表单上有几个Edit组件,并且Form.OnKeyPress过程如下所示:
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char) ;
begin
if Key in ['0'..'9'] then Key := #0
end;
If one of the Edit components has the Focus, and the KeyPreview property of a form is False, this code will not execute. In other words, if the user presses the 5 key, the 5 character will appear in the focused Edit component.
如果“编辑”组件之一具有“ 焦点”,并且窗体的KeyPreview属性为False,则此代码将不会执行。 换句话说,如果用户按下5键,则5个字符将出现在焦点突出的“编辑”组件中。
However, if the KeyPreview is set to True, then the form's OnKeyPress event is executed before the Edit component sees the key that is pressed. Again, if the user has pressed the 5 key, then it assigns the character value of zero to Key to prevent numerical input into the Edit component.
但是,如果KeyPreview设置为True,则在Edit组件看到按下的键之前,将执行窗体的OnKeyPress事件。 同样,如果用户按下5键,则它将零的字符值分配给Key,以防止数字输入到Edit组件中。
翻译自: https://www.thoughtco.com/understanding-keyboard-events-in-delphi-1058213
delphi 键盘按键模拟