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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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
Course of the Month11 days, left to enroll

770 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