VB listen for arrow keys

Posted on 2005-03-07
Medium Priority
Last Modified: 2008-02-01
I have a mature fielded application that i am trying to retrofit. I need to listen for the Left and Right arrow keys fro the keyboard.

The software is a presentation tool and there are several forms or containers playing differnt media formats. When I set keypreview to the master form and try to use the built in keydown or keypress subs they do not always have focu and in most cases never get the message anyway. There is also an optional topmost floating toolbar to interfere with focus.

I have a timer running at 200ms to listen for the mouse curssor hitting an edge where i produce a pop up menu.

Ideally there would be some way to read the keyboard buffer in the timer sub, decide if i need to take a Previous or Next  action and flush the keyboard buffer.
Question by:nphoenix
LVL 32

Accepted Solution

Erick37 earned 1000 total points
ID: 13482495
You can poll the keyboard state in your time with the GetAsyncKeyState API.
More info here:

Private Const VK_RIGHT = &H27
Private Const VK_LEFT = &H25
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

If GetAsyncKeyState(VK_RIGHT) Then
    Debug.Print "Right Arrow"
End If

If GetAsyncKeyState(VK_LEFT) Then
    Debug.Print "Left Arrow"
End If

Author Comment

ID: 13486662
Awesome - that was it exactly. :) Thanks!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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 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…
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…
Suggested Courses

571 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