474,052 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global Keypress Event Handler

Hello everyone!

I am new to VB .Net and currently i'm using vb .net 2005. My question
is: Is there a way to assign a global key handler for a certain key,
like if i press F9 or any other key from any part of my program, a
function or sub would be invoked? I used to do programming in Clipper
and this done achieved by the Setkey( <nKey>, <bFunction) function.
Can this be done in VB .Net?
Thanks in advance!
diego

Oct 23 '08 #1
3 8229
hi,
here is an answer :

Public Class Form1
' If you just want the hot key, with no modifier

' use zero for the fsModifiers value (But this is a BAD IDEA).

Private Const NoModKey As Integer = 0

' Modifier key constants

Private Const MOD_ALT As Integer = 1

Private Const MOD_CONTROL As Integer = 2

Private Const MOD_SHIFT As Integer = 4

Private Const MOD_WIN As Integer = 8

' Value indicating Windows Message is a hot key.

Protected Friend Const WM_HOTKEY As Integer = 786

' Unique ID for the atomic hot key.

Protected Friend hotkeyID As Short

' Register hotkey

Protected Friend Declare Function RegisterHotKey Lib "user32" (ByVal
hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk
As Keys) As Integer

' Add global name for hotkey

Protected Friend Declare Function GlobalAddAtomA Lib "kernel32" (ByVal
lpString As String) As Short

' Delete hotkey atom.

Protected Friend Declare Function GlobalDeleteAto m Lib "kernel32" (ByVal
nAtom As Integer) As Short

' Unregister hotkey.

Protected Friend Declare Function UnregisterHotKe y Lib "user32" (ByVal
hwnd As IntPtr, ByVal id As Integer) As Integer

Private Sub hotkey_Load()

' GlobalAddAtom adds the String to the System global

' atom table, and returns a unique number to identify

' it the atom table.

hotkeyID = GlobalAddAtomA( "GlobalHotKeyFo r_MyUniqueAppNa me")

If hotkeyID = 0 Then

MessageBox.Show ("Unable to generate the requested hotkey unique
ID.", "Error Making Hotkey ID")

Else

' Register the hot key combo used to show the form.

' I used Alt key modifier and the F1 key, Alt + F1,

' but you can use any key combo.

If RegisterHotKey( Me.Handle, hotkeyID, MOD_ALT, Keys.Delete) = 0
Then

MessageBox.Show ("Unable to register the requested hotkey.",
"Error Registering Hotkey")

Else
'mettre la ligne suivante en commentaire pour la production
MessageBox.Show ("The following Hotkey was registered for
this application: " & "Keys: Alt + F1", "Hot Key Registered")

End If

End If

End Sub

Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing

' READ ME:

' You MUST unregister your Hot Key, or your application will leak
memory.

If Me.hotkeyID <0 Then

UnregisterHotKe y(Me.Handle, hotkeyID)

' Also delete the hot key atom.

GlobalDeleteAto m(hotkeyID)

End If

End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows. Forms.Message)

' Check for our Windows Message Hotkey.

If m.Msg = WM_HOTKEY Then

' Do something.

End If

' Return key messages to the application.

MyBase.WndProc( m)

End Sub

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
hotkey_Load()
End Sub
End Class


"Appr3nt1c3 " <gu**********@h otmail.comwrote in message
news:db******** *************** ***********@e38 g2000prn.google groups.com...
Hello everyone!

I am new to VB .Net and currently i'm using vb .net 2005. My question
is: Is there a way to assign a global key handler for a certain key,
like if i press F9 or any other key from any part of my program, a
function or sub would be invoked? I used to do programming in Clipper
and this done achieved by the Setkey( <nKey>, <bFunction) function.
Can this be done in VB .Net?
Thanks in advance!
diego
Oct 23 '08 #2
On 23 Okt, 20:59, "Gillard" <gillard_george s@@@@@@@@@hotma il.com>
wrote:
hi,
here is an answer :

Public Class Form1
* * ' If you just want the hot key, with no modifier

* * ' use zero for the fsModifiers value (But this is a BAD IDEA).

* * Private Const NoModKey As Integer = 0

* * ' Modifier key constants

* * Private Const MOD_ALT As Integer = 1

* * Private Const MOD_CONTROL As Integer = 2

* * Private Const MOD_SHIFT As Integer = 4

* * Private Const MOD_WIN As Integer = 8

* * ' Value indicating Windows Message is a hot key.

* * Protected Friend Const WM_HOTKEY As Integer = 786

* * ' Unique ID for the atomic hot key.

* * Protected Friend hotkeyID As Short

* * ' Register hotkey

* * Protected Friend Declare Function RegisterHotKey Lib "user32" (ByVal
hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk
As Keys) As Integer

* * ' Add global name for hotkey

* * Protected Friend Declare Function GlobalAddAtomA Lib "kernel32" (ByVal
lpString As String) As Short

* * ' Delete hotkey atom.

* * Protected Friend Declare Function GlobalDeleteAto m Lib "kernel32"(ByVa l
nAtom As Integer) As Short

* * ' Unregister hotkey.

* * Protected Friend Declare Function UnregisterHotKe y Lib "user32" (ByVal
hwnd As IntPtr, ByVal id As Integer) As Integer

* * Private Sub hotkey_Load()

* * * * ' GlobalAddAtom adds the String to the System global

* * * * ' atom table, and returns a unique number to identify

* * * * ' it the atom table.

* * * * hotkeyID = GlobalAddAtomA( "GlobalHotKeyFo r_MyUniqueAppNa me")

* * * * If hotkeyID = 0 Then

* * * * * * MessageBox.Show ("Unable to generate the requestedhotkey unique
ID.", "Error Making Hotkey ID")

* * * * Else

* * * * * * ' Register the hot key combo used to show the form.

* * * * * * ' I used Alt key modifier and the F1 key, Alt + F1,

* * * * * * ' but you can use any key combo.

* * * * * * If RegisterHotKey( Me.Handle, hotkeyID, MOD_ALT, Keys.Delete) = 0
Then

* * * * * * * * MessageBox.Show ("Unable to register the requested hotkey.",
"Error Registering Hotkey")

* * * * * * Else
* * * * * * * * 'mettre la ligne suivante en commentaire pour la production
* * * * * * * * MessageBox.Show ("The following Hotkey wasregistered for
this application: " & "Keys: Alt + F1", "Hot Key Registered")

* * * * * * End If

* * * * End If

* * End Sub

* * Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing

* * * * ' READ ME:

* * * * ' You MUST unregister your Hot Key, or your application will leak
memory.

* * * * If Me.hotkeyID <0 Then

* * * * * * UnregisterHotKe y(Me.Handle, hotkeyID)

* * * * * * ' Also delete the hot key atom.

* * * * * * GlobalDeleteAto m(hotkeyID)

* * * * End If

* * End Sub

* * Protected Overrides Sub WndProc(ByRef m As System.Windows. Forms.Message)

* * * * ' Check for our Windows Message Hotkey.

* * * * If m.Msg = WM_HOTKEY Then

* * * * * * ' Do something.

* * * * * * * * * * End If

* * * * ' Return key messages to the application.

* * * * MyBase.WndProc( m)

* * End Sub

* * Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
* * * * * * *hotkey_Load()
* * * * * End Sub
End Class

"Appr3nt1c3 " <guerrero....@h otmail.comwrote in message

news:db******** *************** ***********@e38 g2000prn.google groups.com...
Hello everyone!
I am new to VB .Net and currently i'm using vb .net 2005. My question
is: Is there a way to assign a global key handler for a certain key,
like if i press F9 or any other key from any part of my program, a
function or sub would be invoked? I used to do programming in Clipper
and this done achieved by the Setkey( <nKey>, <bFunction) function.
Can this be done in VB .Net?
Thanks in advance!
diego- Itago ang tekstong may panipi -

-Ipakita ang tekstong may panipi-
Thanks for your reply.

How is it done if my startup object is Sub Main()?

Thanks
Oct 23 '08 #3
On Thu, 23 Oct 2008 01:37:44 -0700 (PDT), Appr3nt1c3
<gu**********@h otmail.comwrote :
>Hello everyone!

I am new to VB .Net and currently i'm using vb .net 2005. My question
is: Is there a way to assign a global key handler for a certain key,
like if i press F9 or any other key from any part of my program, a
function or sub would be invoked? I used to do programming in Clipper
and this done achieved by the Setkey( <nKey>, <bFunction) function.
Can this be done in VB .Net?
You can register a message filter with Application.Add MessageFilter.
Oct 23 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7406
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new KeyPressEventHandler(dgGLBatch_KeyPress); but nothing changed. I was wondering wether a custom table and column style might have an effect on how this works???
3
8732
by: Raul M. Colon | last post by:
Is possible to assign a click event to a button control in a Web form just pressing the return key? Something like windows forms where you can assign this action to a default control. For example, if I press Return, I want to fire the button_click event even when the button is not in focus. Any thoughts? Thanks! Raul
3
3055
by: tafs7 | last post by:
My code below is supposed to email me when an error occurs on the application, but it's not emailing anything. Am I missing something? I know the smtp servers I've tried work. I even added a App_Start handler to see if I could get emailed at all even from the first request of the app, but to no avail. Could someone please help me out? Thanks a lot! --Thiago Web developer AgniTEK
6
4501
by: Niksa Baldun | last post by:
Hi, I am trying to capture data from magnetic stripe reader which is connected via keyboard interface (therefore indistinguishable from normal keyboard input). Basically, I am doing the following (in the Form.Keypress event handler): strCardData &= e.KeyChar e.Handled = True
3
7295
by: Nikolay Evseev | last post by:
Hi, I am trying to trace down the Enter key in my Form.KeyPress event handler. The KeyPreview property is set to false, so I'd assume that all key presses should go through my form's KeyPress event handler, right? Ok. If the focus is set to any other control than a button, then the above event handler fires, but if the focus is on a button, then I've no way to know that the Enter key has been pressed or not, because in that case pressing...
0
1728
by: kryptomoon | last post by:
I have a KeyPress event handler for a MaskedTextBox control. According to the event documentation, if I set e.Handled to 'true' it should discard the input character, but it doesn't. After the event, the TextChanged event is being fired as if the character is accepted. Is this a bug? Do you have any workarounds? Thanks in advance, Murat
3
2147
by: windy | last post by:
I got a question about keypress event on textbox: I found that the keypress event doesn't invoked when i press the "." button on alphabetic keyboard, however if i use numberpad's "." button keypress event is invoked. Any idea and solutions? Thanks.
3
2908
by: thomson | last post by:
Hi All, i do have an website with the URL http://localhost/application/ASEAN-ANZ, Once i hit the application, it goes to the Global.asax. but after that if i tried to change the URL http://localhost/application/en-US, the global.asax is not fired.
2
19310
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I have created for this purpose is derived from class UserControl.
0
10563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11623
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
12058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8727
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
5434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4954
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.