Page 1 of 1

Global Hotkey Make hotkeys which work even when the form in unfocused. Rate Topic: -----

#1 CodingIlliterate  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 32
  • Joined: 28-September 08

Posted 21 March 2009 - 01:53 PM

This tutorial will show you how to make a hotkey which works even when the form is unfocused.

We are going to use the GetAsyncKeyState API to do this.
So first we need to call it.

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer



OK,
Now what our program does is check frequently (very frequently) whether the keys we want are being pressed.
To do this we will use a timer, so drag one out onto your form.

Lets just asume that it is called Timer1.
Now add this code to the Form1.load event.

Timer1.enabled = true
Timer1.interval = 1


This code is just setting the Timer1's properties "Interval" to 1 and "Enabled" to true when the form opens.

Now, count how many keys are pressed when your hotkey is pressed, for this example we will say there are three.
Ctrl - Shift - K.
So declare 3 booleans in the Timer1.Tick event, they can be called whatever you like;
E.g. Poobah or walalalah or one.

Dim crtlkey As Boolean
Dim shiftkey As Boolean
Dim k As Boolean



When that is done we need to set the value of the booleans. This is the part where the keys that need to be pressed are specified.

Remeber, this example uses Ctrl - Shift - K.

ctrlkey = GetAsyncKeyState(Keys.ControlKey)
shiftkey = GetAsyncKeyState(Keys.ShiftKey)
k = GetAsyncKeyState(Keys.K)



You can initialise them if you like, but if you don't know what initialising variables is, don't worry, this way's good. :D

Now we use an If statement to check if the keys are down.
If ctrlkey And shiftkey And k = True Then

		'here is the code that runs when the hotkey is pressed'

end if





So heres our final code.
Public Class Form1
	Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Timer1.enabled = true
Timer1.interval = 1

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Dim crtlkey As Boolean
Dim shiftkey As Boolean
Dim k As Boolean

ctrlkey = GetAsyncKeyState(Keys.ControlKey)
shiftkey = GetAsyncKeyState(Keys.Shiftkey)
k = GetAsyncKeyState(Keys.K)

If ctrlkey And shiftkey And k = True Then

		'Here is the code that runs when the hotkey is pressed'

end if
End Class



Hopefully it worked! :)

What this code does is everytime the timer ticks, which is every millisecond, it checks to see whether the keys you specified are down. If they are it runs the code in the If statement.

Please note that this code was just an example, your hotkey can be as long or as short as you like and the booleans can be called anything.

Dim blerty As Boolean
Dim wadas As Boolean

blerty= GetAsyncKeyState(Keys.S)
wadas = GetAsyncKeyState(Keys.L)

if blerty and wadas = true then

'Here is the code that runs again'

end if




*A Handy Few Notes

Any Button on your keyboard like Control, Shift etc. is
(Keys.ControlKey)
not
(Keys.Control)

I also spent lots of time searching for the Alt Key.
The Alt Key is
(Keys.Menu)


Enjoy :D

Is This A Good Question/Topic? 2
  • +

Replies To: Global Hotkey

#2 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Posted 08 May 2009 - 05:32 AM

Didn't get it to work with the media buttons.
Was This Post Helpful? 0
  • +
  • -

#3 FallenDreamL7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-August 09

Posted 20 August 2009 - 08:43 AM

Thank you very much for your very simple and effective tutorial.

But is there anyway reduce the sensitivity when the hotkeys are being pressed ?? Or perhaps letting the program know that I only pressed the button once, everytime I pressed the hotkey ??

Because it's sorta difficult to use the hotkeys when I wanted to open 1 particular application instead of, 6 of that same particular application and I had to close the other 5 (or more) everytime I used the hotkeys to open the application.

Thank you for your attention.

This post has been edited by FallenDreamL7: 20 August 2009 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 ceasley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-August 09

Posted 28 August 2009 - 10:07 AM

View PostFallenDreamL7, on 20 Aug, 2009 - 07:43 AM, said:

Thank you very much for your very simple and effective tutorial.

But is there anyway reduce the sensitivity when the hotkeys are being pressed ?? Or perhaps letting the program know that I only pressed the button once, everytime I pressed the hotkey ??

Because it's sorta difficult to use the hotkeys when I wanted to open 1 particular application instead of, 6 of that same particular application and I had to close the other 5 (or more) everytime I used the hotkeys to open the application.

Thank you for your attention.



I'm not sure it's the best way, but I used an "Exit Sub" after the event fired. Code is below:

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
		Dim alt As Boolean
		Dim endkey As Boolean

		alt = GetAsyncKeyState(Keys.Menu)
		endkey = GetAsyncKeyState(Keys.End)
		If alt And endkey = True And Me.WindowState = FormWindowState.Minimized Then
			Me.WindowState = FormWindowState.Normal
			Me.ShowInTaskbar = True
			Me.Activate()
			Exit Sub
		End If

		If alt And endkey = True And Me.WindowState = FormWindowState.Normal Then
			writedatabase()
			ComboBox1.Text = ""
			TextBox1.Text = ""
			Me.WindowState = FormWindowState.Minimized
			Me.ShowInTaskbar = False
			Exit Sub
		End If


	End Sub

Was This Post Helpful? 0
  • +
  • -

#5 superbarnie  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 3
  • Joined: 03-November 09

Posted 03 November 2009 - 12:58 AM

you wrote :
Dim crtlkey As Boolean

then u used:
ctrlkey = GetAsyncKeyState(Keys.ControlKey)


i think u did something wrong. i like ur tutorial but i think that u have a typo.
Was This Post Helpful? -2
  • +
  • -

#6 Guest_John*


Reputation:

Posted 19 December 2010 - 02:21 PM

I get this error when I set the value for shiftkey:

A call to PInvoke function 'WindowsApplication2!WindowsApplication1.Form2::GetAsyncKeyState' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Was This Post Helpful? 0

#7 Guest_semih*


Reputation:

Posted 29 December 2010 - 09:24 AM

View PostJohn, on 19 December 2010 - 01:21 PM, said:

I get this error when I set the value for shiftkey:A call to PInvoke function 'WindowsApplication2!WindowsApplication1.Form2::GetAsyncKeyState' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.



I get this error too. I couldnt resolve the problem:(
Was This Post Helpful? 0

#8 Stef-vb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 16-March 11

Posted 16 March 2011 - 02:05 PM

Am writing on a phone so can't be too verbose, basically this is not the way!! This is a half vb6 half .net terribly written example and I would point anyone who need to know how this should be done to google vb.net global hotkeys and visit the social.msdn.com result a few from the top. The above code is basically the least elegant way possible to achieve the global hotkey functionality and owing to it's 1 millisecond timer interval would drastically reduce the performance of a machine on, and the mere suggestion that you would expect, want, or even be able to register 1000 keypresses in a second shows the author level of experience and understanding of programming in gereral and the .net language. This is poor advice and example, blind leading the blind. Apologies to the well meaning author but this code shouldn't be being read as a guide
Was This Post Helpful? 1
  • +
  • -

#9 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Posted 03 May 2012 - 06:09 AM

View Postsuperbarnie, on 03 November 2009 - 08:58 AM, said:

you wrote :
Dim crtlkey As Boolean

then u used:
ctrlkey = GetAsyncKeyState(Keys.ControlKey)


i think u did something wrong. i like ur tutorial but i think that u have a typo.


I've not followed this tutorial, but I can assume GetAsyncKeyState would return a boolean, therefore the dimmed variable is correct.

~huzi
Was This Post Helpful? 0
  • +
  • -

#10 asalim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 13

Posted 13 July 2013 - 10:46 AM

Hey your code is working but there is a mistake i tested your code for contrl + shift +k but when i press shift button alone that code exicuting.. thats really bad

This post has been edited by macosxnerd101: 13 July 2013 - 11:05 AM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#11 asalim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 13

Posted 14 July 2013 - 07:54 AM

hiii please help me with this


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim ctrlkey As Boolean
        Dim shiftkey As Boolean
        Dim altkey As Boolean
        Dim k As Boolean


        ctrlkey = GetAsyncKeyState(Keys.ControlKey)
        shiftkey = GetAsyncKeyState(Keys.ShiftKey)
        altkey = GetAsyncKeyState(Keys.Menu)
        k = GetAsyncKeyState(Keys.K)

        If altkey And k = True Then


            Me.Show()
            Me.Opacity = 100
            Me.ShowInTaskbar = True

        End If




when i press alt key alone the code is working what to do?
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,996
  • Joined: 12-December 12

Posted 14 July 2013 - 09:00 AM

I would read Stef-vb 's post #8 and look for another, better, tutorial.
Was This Post Helpful? 0
  • +
  • -

#13 asalim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 13

Posted 14 July 2013 - 09:29 AM

can you help me?

hiii please help me with this when i press alt key alone the code is working what to do?

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim ctrlkey As Boolean
        Dim shiftkey As Boolean
        Dim altkey As Boolean
        Dim k As Boolean


        ctrlkey = GetAsyncKeyState(Keys.ControlKey)
        shiftkey = GetAsyncKeyState(Keys.ShiftKey)
        altkey = GetAsyncKeyState(Keys.Menu)
        k = GetAsyncKeyState(Keys.K)

        If altkey And k = True Then


            Me.Show()
            Me.Opacity = 100
            Me.ShowInTaskbar = True

        End If



Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,996
  • Joined: 12-December 12

Posted 14 July 2013 - 09:44 AM

I quickly attempted this code and received a PInvoke StackImbalance error, but I disagree with the way the author has approached this anyway.

Again, I would find a better example or tutorial on the internet. We have many good tutorials here but this isn't one of them.

http://social.msdn.m...hotkey-in-vbnet

This post has been edited by andrewsw: 14 July 2013 - 09:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1