Results 1 to 3 of 3

Thread: System-wide Hotkey

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    System-wide Hotkey

    This code lets you use hotkeys from your program. Even if your app is not in focus the hotkey will work. This one is CTRL + A
    Also to change the Keys you will probably need the Virtual Key codes
    They Are Here
    VB.NET Code:
    1. Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    2.     Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
    3.  
    4.     Private Const WM_HOTKEY As Integer = &H312
    5.  
    6.     Private Const MOD_ALT As Integer = &H1
    7.     Private Const MOD_CONTROL As Integer = &H2
    8.     Private Const MOD_SHIFT As Integer = &H4
    9.  
    10.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    11.         UnregisterHotKey(Me.Handle, 0)
    12.     End Sub
    13.  
    14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15.         RegisterHotKey(Me.Handle, 0, MOD_CONTROL, Asc("A"))
    16.     End Sub
    17.  
    18.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    19.  
    20.         If m.Msg = WM_HOTKEY Then
    21.            If m.WParam = 0 Then
    22.             'CODE HERE
    23.            End if
    24.         End If
    25.         MyBase.WndProc(m)
    26.  
    27.     End Sub

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: System-wide Hotkey

    I really dont want to seem like I'm just hating on all your codebank posts and I dont want to discourage you from posting things in here, but... there are already a few threads in here that show how to do this - just do a search in the codebank before posting something here as there is no point duplicating things really. I just searched for "hotkey" and found this thread which has a nice managed .NET class that wraps up the functionality of the APIs you mentioned: http://www.vbforums.com/showthread.php?t=542976

    PS using Ctrl+A as a system wide hotkey is a bad idea as loads of programs use that for selecting all text. I'm sure you were only using it as an example though.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: System-wide Hotkey

    I'm also not wanting to seem like I'm hating Emcrank's codebank threads, but most of them are basically just copies of other codebank threads, some of which have been on the forum for years already.

    Like the move borderless form thread, it's identical (even the flaws of moving a borderless form that way) to the other one that was mentioned in Emcrank's codebank thread.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width