ComboBox.KeyPress 事件 (Access)ComboBox.KeyPress event (Access)

KeyPress 事件發生於使用者按下及放開按鍵或組合鍵對應的 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. 如果您使用 SendKeys 巨集指令的巨集或 Visual Basic 中的 SendKeys 陳述式傳送至表單或控制項 ANSI 按鍵,也會發生此事件。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

運算式KeyPress(KeyAscii)expression.KeyPress (KeyAscii)

_運算式_代表**ComboBox** 物件的變數。expression A variable that represents a ComboBox object.

參數Parameters

名稱Name 必要/選用Required/Optional 資料類型Data type 描述Description
KeyAsciiKeyAscii 必要Required IntegerInteger 會傳回數字 ANSI 按鍵代碼。Returns a numeric ANSI key code. KeyAscii 引數使用傳址方式傳遞;變更引數會傳送不同的字元至物件。The KeyAscii argument is passed by reference; changing it sends a different character to the object. KeyAscii 引數設為 0 取消按鍵動作,讓物件不會識別按鍵已經按下。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 屬性設定為 [是]。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. 例如,您可能想組合鍵 Ctrl + X 一律在表單上執行相同的動作。For example, you may want the key combination Ctrl+X to always perform the same action on a form.

如果您按下並按住 ANSI 鍵, KeyDownKeyPress事件替代便會重複替換 (KeyDownKeyPressKeyDownKeyPress等等) 直到您放開按鍵,然後再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, and then the KeyUp event occurs.

KeyPress事件可以包含任何列印鍵盤字元、 結合從標準的英文字母字元或特殊字元、 Ctrl 鍵並輸入] 或 [Backspace 鍵。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. 您可以使用的KeyDownKeyUp事件程序來處理任何無法辨識的KeyPress事件,例如功能鍵、 巡覽鍵,以及這些鍵盤輔助按鍵 (Alt、 shift 鍵或 Ctrl 鍵) 的任何組合的按鍵動作。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). 不同於 KeyDownKeyUp 事件 KeyPress 事件不會指出鍵盤 ; 實體狀態而是表示會對應至按下的按鍵或組合鍵的 ANSI 字元。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 字元組,但不的 Delete 鍵。The Backspace key is part of the ANSI character set, but the Delete key isn't. 如果您使用退格鍵刪除控制項中的字元,您會造成KeyPress事件;如果您使用 Delete 鍵,您不支援。If you delete a character in a control by using the Backspace key, you cause a KeyPress event; if you use the Delete key, you don't.

當您按下或傳送 ANSI 鍵,就會發生 KeyDownKeyPress 事件。The KeyDown and KeyPress events occur when you press or send an ANSI key. KeyUp 事件發生在任何事件之後按下或傳送按鍵而造成的控制項。The KeyUp event occurs after any event for a control caused by pressing or sending the key. 如果按鍵使焦點從一個控制項移到另一個控制項, KeyDown 事件發生的第一個控制項、 KeyPressKeyUp 事件發生的第二個控制項時。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 (適用於控制項)KeyPress (for the control)
  • BeforeInsert (適用於表單中的新記錄)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.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

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.