Registering hotkeys in Applications using VB

Source: Internet
Author: User
Tags integer modifiers
Program ' form
Option Explicit

Private Sub Form_Load ()
DIM ret as Long
' Record the original window program address
Prewinproc = GetWindowLong (Me.hwnd, GWL_WNDPROC)
' Replace the original window program with a custom program
ret = SetWindowLong (Me.hwnd, GWL_WNDPROC, AddressOf WNDPROC)
Idhotkey = 1 ' in the range &h0000 through &HBFFF
modifiers = Mod_alt ' secondary key is ALT
UVirtKey1 = Vbkeyq ' Registered hotkey for Alt+q
' Register Hotkey
ret = RegisterHotKey (Me.hwnd, Idhotkey, modifiers, uVirtKey1)
IF ret = 0 Then
MsgBox "Registration Hotkey failed, please use other hotkey!", vbcritical, "error"
End If
End Sub

Private Sub form_queryunload (Cancel As Integer, UnloadMode as Integer)
DIM ret as Long
' Cancel the interception of the message and send it to the original window program
ret = SetWindowLong (Me.hwnd, GWL_WNDPROC, Prewinproc)
Call Unregisterhotkey (Me.hwnd, UVirtKey1)
End Sub

' Module

' The following programs are placed in the module
Option Explicit

Declare Function setwindowlong Lib "user32" Alias "Setwindowlonga" (ByVal hWnd as Long, ByVal nindex as Long, ByVal DWNEWL Ong as long) as long
Declare Function getwindowlong Lib "user32" Alias "Getwindowlonga" (ByVal hWnd as Long, ByVal nindex as long) as long
Declare Function callwindowproc Lib "user32" Alias "Callwindowproca" (ByVal Lpprevwndfunc as Long, ByVal hWnd as Long, byv Al Msg as Long, ByVal WParam as Long, ByVal LParam as long) as long
Declare Function registerhotkey Lib "user32" (ByVal hWnd as long, ByVal ID as long, ByVal fsmodifiers as Long, ByVal VK as Long) as Long
Declare Function unregisterhotkey Lib "user32" (ByVal hWnd as long, ByVal ID as long) as long

Public Const Wm_hotkey = &h312
Public Const Mod_alt = &h1
Public Const Mod_control = &h2
Public Const Mod_shift = &h4
Public Const Gwl_wndproc = (-4)

Public Prewinproc as Long
Public modifiers as long, uVirtKey1 as long, idhotkey as Long

Private Type Talong
ll as Long
End Type

Private Type T2int
Lword as Integer
Hword as Integer
End Type

Public Function WndProc (ByVal hWnd as Long, ByVal Msg as Long, ByVal WParam as Long, ByVal LParam as long) as long
Dim LP as Talong, I2 as T2int

If MSG = Wm_hotkey Then
If WParam = Idhotkey Then
Lp.ll = LParam
LSet i2 = LP
If (I2.lword = modifiers) and I2.hword = UVirtKey1 Then
Form1.visible = Not form1.visible
End If
End If
End If
' Call the original program if it is not a hotkey information
WndProc = CallWindowProc (Prewinproc, HWnd, MSG, WParam, LParam)
End Function



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.