Trapping keystroke in a form

mcdhappy80

Registered User.
Local time
Today, 10:58
Joined
Jun 22, 2009
Messages
347
I want to execute a code on specific key combination when I'm on one of the forms in my database.
Lets say I want to press the "P" key (or any other) and then when I do that I want to make some controls on my form visible.
Can someone provide me the code for this, or a link that can help me?
Also would there be some difference if You try to trap one keystroke and a combination of two or more keys pressed?

Thank You
 
there are things called HOTKEYS in access. you may want to try that. for example, if you create a control in access, and you write this in the caption of a label or button...
Code:
&h
...you can press Alt+H as a hotkey, and write a procedure behind the form that will do anything you want.

as a second option, you can use the "onkeypress" event for the form or another control and check the ASCII value of the key pressed, and then do something if the check is true...
 
First you have to set the form's KeyPreview property to yes, so the keystrokes will be evaluated by the form, before being evaluated by the controls. This property can be founded under the event tab in the form's property window. From your description, I would suggest using a key combination rather a single key stroke. Use the KeyDown event to trap the special keys like F10 and key combinations.
 
So, I enable Key Preview to Yes in Properties Event tab, and then put the code in On Key Down, or On Key Press event of the form?
Whit what code do I evaluate which key is pressed?
Can someone write an example how to evaluate if ALT + B keys are pressed?
Thank You.
 
[SOLVED] Trapping keystroke in a form

I've solved the problem by setting Key Preview to Yes and putting the following code in forms On Key Up event:

Code:
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  
Dim dirka As Integer
dirka = KeyCode

'MsgBox "Key code= " & KeyCode & vbCrLf & "Shift= " & Shift

If dirka = 66 And Shift = 5 Then
    MsgBox "Bekap mod uspesno aktiviran.", vbInformation, "Bekap Mod"
    
    Me.boxBekKut.Visible = True
    Me.lblBekOp.Visible = True 
    Me.btnZapBack.Visible = True
    Me.btnUgoBack.Visible = True
    Me.btnOdmBack.Visible = True

End If
End Sub

This code works for ALT + SHIFT + B key combination.

When You press SHIFT + ALT the value Shift equals 5 (4 + 1 if I remember correctly) and value of B key is 66, and those two parameters we evaluate in the If statement.

If You wanna see which key has which value just re-enable the MsgBox function which is commented out.

Cheers.
 

Users who are viewing this thread

Back
Top Bottom