Solved

Keydown or KeyPress within module

Posted on 2004-03-30
2
989 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now