Table of contents
TOC
折叠目录
展开目录

ListBox.KeyPress 事件 (访问)ListBox.KeyPress Event (Access)

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

当用户按下并释放一个键或组合键对应于 ANSI 代码窗体或控件具有焦点时,将发生KeyPress事件。通过使用 SendKeys 操作的宏或 Visual Basic 中的SendKeys语句的 ANSI 键击发送到窗体或控件,也会发生此事件。The KeyPress event occurs when the user presses and releases a key or key combination that corresponds to an ANSI code while a form or control has the focus. This event also occurs if you send an ANSI keystroke to a form or control by using the SendKeys action in a macro or the SendKeys statement in Visual Basic.

语法Syntax

表达式按键(* * KeyAscii* )expression. **KeyPress( ** KeyAscii* )

表达式_表示一个列表框对象变量。_expression A variable that represents a ListBox object.

参数Parameters

名称Name必需/可选Required/Optional数据类型Data Type说明Description
KeyAsciiKeyAscii必需RequiredIntegerInteger返回数字的 ANSI 键代码。将_KeyAscii_参数是通过引用; 传递更改该对象发送一个不同的角色。将_KeyAscii_参数设为 0 可取消键击,这样,对象不会认为已按下某个键。Returns a numeric ANSI key code. The KeyAscii argument is passed by reference; changing it sends a different character to the object. Setting the KeyAscii argument to 0 cancels the keystroke so that the object doesn't recognize that a key was pressed.

注解Remarks

若要在该事件发生时运行宏或事件过程,请将OnKeyPress属性设置为宏的名称或 [事件过程]。To run a macro or event procedure when this event occurs, set the OnKeyPress property to the name of the macro or to [Event Procedure].

获得焦点的对象将接收到所有的键击,而窗体只有在它没有控件或它所有可见的控件都失效时才能获得焦点。The object with the focus receives all keystrokes. A form can have the focus only if it has no controls or all its visible controls are disabled.

窗体也能接收所有的键盘事件,甚至包括那些对控件发生,如果将窗体的KeyPreview属性设置为是。通过设置此属性值,所有键盘事件首先都发生该窗体,然后该控件具有焦点。您可以对无论哪个控件获得焦点,窗体中按下的特定键作出响应。例如,您可能希望组合键 CTRL + X 总在窗体上执行相同的操作。A form will also receive all keyboard events, even those that occur for controls, if you set the KeyPreview property of the form to Yes. With this property setting, all keyboard events occur first for the form, and then for the control that has the focus. You can respond to specific keys pressed in the form, regardless of which control has the focus. For example, you may want the key combination CTRL+X to always perform the same action on a form.

如果您按下并按住一个 ANSI 键,则KeyDownKeyPress事件交替反复 ( KeyDown按键KeyDown按键,等等) 释放键,则KeyUp事件之前发生。If you press and hold down an ANSI key, the KeyDown and KeyPress events alternate repeatedly ( KeyDown, KeyPress, KeyDown, KeyPress, and so on) until you release the key, then the KeyUp event occurs.

KeyPress事件可涉及任何可打印的键盘字符、 结合标准字母表中的字母字符或特殊字符,CTRL 键和 enter 键或 BACKSPACE 键。KeyDownKeyUp事件过程可用于处理任何不识别的按键事件,如功能键、 导航键和键盘修饰符 (ALT、 shift 键或 CTRL 键) 与其中的任何组合的键击。与KeyDown事件和KeyUp事件, KeyPress事件并不表明物理状态的键盘;相反,它表示对应于所按下的键或键组合的 ANSI 字符。A KeyPress event can involve any printable keyboard character, the CTRL key combined with a character from the standard alphabet or a special character, and the ENTER or BACKSPACE key. You can use the KeyDown and KeyUp event procedures to handle any keystroke not recognized by the KeyPress event, such as function keys, navigation keys, and any combinations of these with keyboard modifiers (ALT, SHIFT, or CTRL keys). Unlike the KeyDown and KeyUp events, the KeyPress event doesn't indicate the physical state of the keyboard; instead, it indicates the ANSI character that corresponds to the pressed key or key combinations.

KeyPress将解释的大写和小写的每个字符作为不同的键代码,因此,作为两个字符。KeyPress interprets the uppercase and lowercase of each character as separate key codes and, therefore, as two separate characters.

请注意 退格键属于 ANSI 字符集,但 DEL 键不是。如果您使用退格键删除控件中的字符,则会导致KeyPress事件;如果您使用 DEL 键,,不需要。Note The BACKSPACE key is part of the ANSI character set, but the DEL key isn't. If you delete a character in a control by using the BACKSPACE key, you cause a KeyPress event; if you use the DEL key, you don't.

KeyDownKeyPress事件发生在按或发送一个 ANSI 键。任何事件之后, KeyUp事件发生按下或发送键引起控件。如果键击导致焦点从一个控件移动到另一个控件,将发生KeyDown事件有关的第一个控件的按键KeyUp事件发生的第二个控件。The KeyDown and KeyPress events occur when you press or send an ANSI key. The KeyUp event occurs after any event for a control caused by pressing or sending the key. If a keystroke causes the focus to move from one control to another control, the KeyDown event occurs for the first control, while the KeyPress and KeyUp events occur for the second control.

例如,如果移到一个新记录并在记录的第一个控件中键入字符,则下列事件发生:For example, if you go to a new record and type a character in the first control in the record, the following events occur:

当前(新的记录)Current (for the new record)

?

输入(适用于新记录中的第一个控件)Enter (for the first control in the new record)

?

GotFocus(用于控制)GotFocus (for the control)

?

KeyDown(用于控制)KeyDown (for the control)

?

按键(用于控制)KeyPress (for the control)

?

插入(用于在窗体中的新记录)BeforeInsert (for the new record in the form)

?

更改(对于如果是文本框或组合框的控件)Change (for the control if it's a text box or combo box)

?

KeyUp(用于控制)KeyUp (for the control)

示例Example

下面的示例在向文本框中键入文本时将所输入的文本转换为大写,每键入一个字符就转换一个。The following example converts text entered in a text box to uppercase as the text is typed in, one character at a time.

若要尝试运行该示例,请将以下事件过程添加到一个窗体中,该窗体包含一个名为 ShipRegion(发货地区)的文本框。To try the example, add the following event procedure to a form that contains a text box named ShipRegion.

Private Sub ShipRegion_KeyPress(KeyAscii As Integer) 
 Dim strCharacter As String 

 ' Convert ANSI value to character string. 
 strCharacter = Chr(KeyAscii) 
 ' Convert character to upper case, then to ANSI value. 
 KeyAscii = Asc(UCase(strCharacter)) 
End Sub

另请参阅See also

概念Concepts

ListBox 对象ListBox Object

© 2018 Microsoft