8 Replies - 3140 Views - Last Post: 19 March 2012 - 11:18 PM Rate Topic: -----

#1 PNJLj   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 237
  • Joined: 18-May 09

Global Hotkey Working .. But Not Working!

Posted 17 March 2012 - 05:31 AM

Hi there, so i have implemented a global Hotkey into my code but the problem is that when i move the focus from the form to IE(For Example) i cannot use the Hotkey from my application. Everything is working great if i was to stay int the form but thats not the purpose of this code. Could someone please give guidance. Thankyou.

Private Sub frmMain_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 9, MOD_ALT, VK_F9)
    End Sub
    Private Sub frmMain_FormClosing(ByVal sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        UnregisterHotKey(Me.Handle, 9)
    End Sub
Public Const MOD_ALT As Integer = &H1
    Public Const VK_NUMPAD1 As Integer = &H61
    Public Const VK_F9 As Integer = &H120
    Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
    Public Const WM_HOTKEY As Integer = &H312

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_HOTKEY Then
            btnEMail_Click(Me, EventArgs.Empty)
        End If
        MyBase.WndProc(m)
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Global Hotkey Working .. But Not Working!

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Global Hotkey Working .. But Not Working!

Posted 17 March 2012 - 06:40 AM

In the form designer.... Property pallet... KeyPreview property. Set it to true
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Global Hotkey Working .. But Not Working!

Posted 17 March 2012 - 06:51 AM

The signature is wrong for the API call,
where it said Integer => us It16 where said long use Int32

Integer and Long are a different bit width to what there were in the vb6 days
Was This Post Helpful? 1
  • +
  • -

#4 PNJLj   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 237
  • Joined: 18-May 09

Re: Global Hotkey Working .. But Not Working!

Posted 17 March 2012 - 06:04 PM

sorry adam, i cannot see what you are talking about, i tried changing all the Integer types to Int32 but no luck.

tlhIn`toq - already had that property set, any other suggestions?
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Global Hotkey Working .. But Not Working!

Posted 17 March 2012 - 06:47 PM

Integer in vb6 days was 16bits long, not 32bits.
Long in vb6 days was 32 bits long, not 64bits.

Integer and Long are Platform dependant if 32 bit then Integer =32,Long = 64 (if its a 64 bit the double those bits)

The Type Int16 is 16bit on all platforms.


Also all the APIs return a value, which is use to return if the call was a success or not. What does yours return?
Was This Post Helpful? 1
  • +
  • -

#6 PNJLj   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 237
  • Joined: 18-May 09

Re: Global Hotkey Working .. But Not Working!

Posted 17 March 2012 - 10:14 PM

what does my return??? < i am guessing you are talking about what does the button do. here that is

Private Sub btnEMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEMail.Click
        If txtEMail.Text = "" Then
            MessageBox.Show("Please Enter An E-Mail Address", "E-Mail??", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            txtEMail.Copy() '<-This is what i want sent to the Clipboard so that i can recall it later
        End If
    End Sub


I sort of understand your mean in your last post but not completly. What is it that i need to change exactly? If you could elaborate it would be very much appreciated.
Was This Post Helpful? 0
  • +
  • -

#7 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Global Hotkey Working .. But Not Working!

Posted 17 March 2012 - 10:46 PM

All those Windows API calls you are making, well *ALMOST* every API call in windows returns a value on success and failure.

For instance, RegisterHotKey if the call succeeds then it returns a nonzero value. If it fails for some reason, it will return 0 and you will have to call GetLastError to find the reason it failed.
Was This Post Helpful? 0
  • +
  • -

#8 PNJLj   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 237
  • Joined: 18-May 09

Re: Global Hotkey Working .. But Not Working!

Posted 18 March 2012 - 02:07 AM

i dont believe i have anything like that incorporated. could you please give an example of how it should look? as i said, the only code that i have is in the first post. i dont have any error checking.
Was This Post Helpful? 0
  • +
  • -

#9 PNJLj   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 237
  • Joined: 18-May 09

Re: Global Hotkey Working .. But Not Working!

Posted 19 March 2012 - 11:18 PM

OKay so i have this working. It works no matter what i am doing within the windows environment which is Great and exactly what i wan. However is there anyway that i can have 3 HotKeys to press instead of just the 2. At the moment i have Alt+Numpad1 which will do what i need. But i want to add Control into that so that my Hotkeys would be Alt+Ctrl+NumPad1.

Private Sub frmMain_FormClosing(ByVal sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        UnregisterHotKey(Me.Handle, 9)
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 9, MOD_ALT, VK_Numpad1)
    End Sub

    Public Const MOD_ALT As System.Int16 = Keys.Menu
    Public Const VK_Numpad1 As System.Int16 = Keys.Numpad1
    Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As System.Int16, ByVal fsModifiers As System.Int16, ByVal vk As System.Int16) As System.Int16
    Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As System.Int16) As System.Int16
    Public Const WM_HOTKEY As Integer = &H312

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_HOTKEY Then
            btnEMail_Click(Me, EventArgs.Empty)
        End If
        MyBase.WndProc(m)
    End Sub


I tried to change this piece of code
Private Sub frmMain_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 9, MOD_ALT, VK_Numpad1)
    End Sub

TO

Private Sub frmMain_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 9, MOD_ALT, VK_Control, VK_Numpad1)
    End Sub


also creating a new Public Const to allow for the change. I have however had no luck! Any Ideas?

AdamSpeight <- I get what you mean now. thankyou.




OK - For SomeReason This IS Now Not Working Again. I can Use It Once Then It Wont work again after i copy something else from somewhere else!, this is really frustrationg me now.

This post has been edited by PNJLj: 19 March 2012 - 11:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1