KeyDown Event

14 May

From:    “Sofyan Efendi” <sofyan@madinah.cc>
Date:    Tue, 22 Aug 2006 11:05:00 +0700

Event KeyDown terjadi ketika user menekan suatu tombol pada keyboard pada saat user sedang aktif (focus) pada suatu form atau control. Syntax:

Private Sub object_KeyDown( KeyCode As Integer, Shift As Integer)

Penjelasan:

– Object: Nama Form atau control di dalam Form.

– KeyCode: Kode tombol, misalnya vbKeyF1 berarti tombol F1 atau vbKeyHome berarti tombol HOME key). Untuk menjaga suatu form atau control dari penekanan suatu tombol, dapat mensetting KeyCode ke 0.

Shift: Yakni penekanan tombol SHIFT, CTRL, dan ALT saat event ini dilakukan. Isilah dengan constants sbb:

. acShiftMask -> The bit mask for the SHIFT key.
. acCtrlMask -> The bit mask for the CTRL key.
. acAltMask -> The bit mask for the ALT key.

Urutan event ini adalah:

KeyDown -> KeyPress -> KeyUp

Jika ditambah dengan event Change, maka urutannya adalah:

KeyDown -> KeyPress -> Change -> KeyUp

Jika event KeyDown menyebabkan berpindahnya focus ke control yang lain, maka urutan event nya adalah:

– First control:
KeyDown -> BeforeUpdate -> AfterUpdate -> Exit -> LostFocus

– Second control:
Enter -> GotFocus -> KeyPress -> KeyUp

Contoh:

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

Wassalaamu’alaikum Warahmatullahi Wabarakatuh,
Sofyan Efendi
– AccessWeb <http://www35.websamba.com/accessweb&gt;
– OPI-Homepage <http://www.geocities.com/opi1978&gt;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: