Solved

Keydown or KeyPress within module

Posted on 2004-03-30
2
994 Views
Last Modified: 2010-07-27
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
Comment
Question by:ingenito
2 Comments
 
LVL 11

Accepted Solution

by:
YohanShminge earned 50 total points
ID: 10719364
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
 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 10720168
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question