Solved

EventHandler that listens for a specific keystroke?

Posted on 2008-06-16
3
428 Views
Last Modified: 2012-06-27
Hi
first of all: I use VB.net in Visual studio 2005

I am trying to make an eventhandler that waits (listen) for a specific keystroke which triggers a Sub() where some tasks are performed. How can that be done? For example i wan't to program a script that opens Internet explorer when i press "arrow down" key , opens Frontpage when i press "arrow up" opens excel when i press "arrow left" ?
How can that be done?
0
Comment
Question by:soriega
  • 3
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
You need to specify WHEN the keystroke should be captured:

(1) Only when your app has FOCUS.
(2) At ANY time, regardless of whether your app is in the foreground or not.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 200 total points
Comment Utility
For (2), you can do something like this:
Public Class Form1
 

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

        Select Case keyData

            Case Keys.Up

                Debug.Print("Up")
 

            Case Keys.Down

                Debug.Print("Down")
 

            Case Keys.Left

                Debug.Print("Left")
 

            Case Keys.Right

                Debug.Print("Right")
 

        End Select
 

        Return MyBase.ProcessCmdKey(msg, keyData)

    End Function
 

End Class

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
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…

763 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