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

KeyDown 事件發生於使用者按下按鍵時在表單或控制項有焦點。The KeyDown event occurs when the user presses a key while a form or control has the focus. 如果將按鍵傳送至表單或控制項的使用 SendKeys 巨集指令的巨集或 Visual Basic 中的 SendKeys 陳述式,也會發生此事件。This event also occurs if you send a keystroke to a form or control by using the SendKeys action in a macro or the SendKeys statement in Visual Basic.

語法Syntax

運算式KeyDown(KeyCode, Shift)expression.KeyDown (KeyCode, Shift)

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

參數Parameters

名稱Name 必要/選用Required/Optional 資料類型Data type 描述Description
KeyCodeKeyCode 必要Required IntegerInteger 按鍵碼, 例如vbKeyF1 (F1 鍵) 或vbKeyHome (Home 鍵)。A key code, such as vbKeyF1 (the F1 key) or vbKeyHome (the Home key). 若要指定按鍵碼,請使用物件瀏覽器中顯示的內部常數。To specify key codes, use the intrinsic constants shown in the Object Browser. 您可以將_KeyCode_設定為 0, 以防止物件接收按鍵。You can prevent an object from receiving a keystroke by setting KeyCode to 0.
ShiftShift 必要Required IntegerInteger Shift、Ctrl 和 ALT 鍵在事件發生時的狀態。The state of the Shift, Ctrl, and Alt keys at the time of the event. 如果您需要測試_Shift_引數, 可以使用下列其中一個內部常數做為位元遮罩:If you need to test for the Shift argument, you can use one of the following intrinsic constants as bit masks:
  • acShiftMask Shift 鍵的位元遮罩。acShiftMask The bit mask for the Shift key.

  • acctrlmask ctrl 鍵 Ctrl 鍵的位元遮罩。acCtrlMask The bit mask for the Ctrl key.

  • acaltmask alt 鍵 ALT 鍵的位元遮罩。acAltMask The bit mask for the Alt key.

註解Remarks

KeyDown事件僅適用于表單及表單上的控制項, 而不適用於報表上的控制項。The KeyDown event applies only to forms and controls on a form, and not to controls on a report.

發生這些事件時執行巨集或事件程序,將 OnKeyDown 屬性設定為巨集名稱或 [事件程序]。To run a macro or event procedure when these events occur, set the OnKeyDown property to the name of the macro or to [Event Procedure].

對於這兩個事件而言,取得焦點的物件會接收所有的按鍵動作。For both events, 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.

如果按住按鍵, keydownKeyPress事件會重複替換 (keydownKeyPressKeyDownKeyPress等等), 直到您放開按鍵為止, 然後是KeyUp事件當.If you press and hold down a 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.

雖然 KeyDown 事件發生時所按下大部分的按鍵,其通常用於識別或區分:Although the KeyDown event occurs when most keys are pressed, it is typically used to recognize or distinguish between:

  • 擴充字元按鍵,例如功能鍵。Extended character keys, such as function keys.

  • 導覽鍵, 例如 Home、End、PgUp、PgDn、向上鍵、向下鍵、向右鍵、向左鍵和 Tab 鍵。Navigation keys, such as Home, End, PgUp, PgDn, Up arrow, Down arrow, Right arrow, Left arrow, and Tab.

  • 按鍵和標準鍵盤修飾符 (Shift、Ctrl 或 ALT 鍵) 的組合。Combinations of keys and standard keyboard modifiers (Shift, Ctrl, or Alt keys).

  • 數字鍵台及鍵盤數字鍵。The numeric keypad and keyboard number keys.

當您按下不會發生 KeyDown 事件:The KeyDown event does not occur when you press:

  • 如果表單有命令按鈕, 且其預設屬性設定為 [是], 則輸入金鑰。The Enter key if the form has a command button for which the Default property is set to Yes.

  • 如果表單具有命令按鈕, 且Cancel屬性設定為 [是] 時, 則為 Esc 鍵。The Esc key if the form has a command button for which the Cancel property is set to Yes.

當您按下或傳送 ANSI 鍵,就會發生 KeyDown 事件。The KeyDown event occurs 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.

若要找出按鍵所對應的 ANSI 字元,使用 KeyPress 事件。To find out the ANSI character corresponding to the key pressed, use the KeyPress event.

如果強制回應對話方塊顯示時按下或傳送按鍵, KeyDownKeyPress 事件會發生,但不會發生 KeyUp 事件。If a modal dialog box is displayed as a result of pressing or sending a key, the KeyDown and KeyPress events occur, but the KeyUp event doesn't occur.

範例Example

下列範例會判斷您是否已按下 Shift、Ctrl 或 ALT 鍵。The following example determines whether you have pressed the Shift, Ctrl, or Alt key.

若要嘗試此範例, 請在包含名為KeyHandler的文字方塊的表單上新增下列事件程序。To try the example, add the following event procedure to a form containing a text box named KeyHandler.

Private Sub KeyHandler_KeyDown(KeyCode As Integer, _ 
     Shift As Integer) 
    Dim intShiftDown As Integer, intAltDown As Integer 
    Dim intCtrlDown As Integer 
 
    ' Use bit masks to determine which key was pressed. 
    intShiftDown = (Shift And acShiftMask) > 0 
    intAltDown = (Shift And acAltMask) > 0 
    intCtrlDown = (Shift And acCtrlMask) > 0 
    ' Display message telling user which key was pressed. 
    If intShiftDown Then MsgBox "You pressed the Shift key." 
    If intAltDown Then MsgBox "You pressed the Alt key." 
    If intCtrlDown Then MsgBox "You pressed the Ctrl key." 
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.