• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1005
  • Last Modified:

Keydown or KeyPress within module

Hello,

I have a form that calls a function contained within a module when a button is pressed.  Is there anyway from within the module to capture and handle the Keydown or KeyPress events?  Thanks.

Mike
0
ingenito
Asked:
ingenito
1 Solution
 
YohanShmingeCommented:
You can use the GetAsyncKeyState API to get the state of a single key:

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

If GetAsyncKeyState(vbKeyEscape) Then
   'Do some stuff
End If
0
 
JMoon5FTMCommented:
GetAsyncKeyState is good if you need to check the state at the time the procedure is called.

If you want to know about future changes...well, you can't use a standard module.  The best you could do would be to put the relevant code in a class module, and create an instance of the class from the standard module.

'Class code for capturing events - MyEventCapture:

Public WithEvents MyForm as Form

Private Sub MyForm_KeyPress(KeyAscii as Integer)
   'Do something
End Sub

'Standard module code

Private Capture as New MyEventCapture

Sub DoSomething(WithForm as Form)
   Set Capture.MyForm = WithForm
End Sub
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now