Global Keypress Event Handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Appr3nt1c3

    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

  • Gillard

    #2
    Re: Global Keypress Event Handler

    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 " <guerrero.rey@h otmail.comwrote in message
    news:dbef2a74-5565-4a00-be12-623d6582a381@e3 8g2000prn.googl egroups.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
    >

    Comment

    • Appr3nt1c3

      #3
      Re: Global Keypress Event Handler

      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:dbef2a74-5565-4a00-be12-623d6582a381@e3 8g2000prn.googl egroups.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

      Comment

      • Jack Jackson

        #4
        Re: Global Keypress Event Handler

        On Thu, 23 Oct 2008 01:37:44 -0700 (PDT), Appr3nt1c3
        <guerrero.rey@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.

        Comment

        Working...