Solved

Keydown or KeyPress within module

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to open Waze.com/livemap from address saved in DB? 26 206
Question about INSTR  - need to extract data MS ACCESS query 6 58
Added a column screws up code 5 63
Help me. 3 60
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

792 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