Program the use of system hotkeys {demonstrate registerhotkey () and unregisterhotkey ()}

Source: Internet
Author: User

PurpleendurerOriginal

Registerhotkey ()
======================================
Function function: This function defines a system-wide hot key.

Function prototype: bool registerhotkey (hwnd, intid, uint fsmodifiers, uint VK );

Parameter description:
Hwnd: The Window handle for receiving the wm_hotkey message generated by the hotkey. If this parameter is null, The wm_hotkey message passed to the calling thread must be processed in the message loop.
ID: The identifier that defines the hotkey. Other hotkeys in the call thread cannot use the same identifier. The application must define a value in the 0x0000-0xbfff range. A shared dynamic link library (DLL) must define a value in the 0xc000-0xffff range returned by the value of the globalglobaladdatom function ). To avoid conflicts with hotkeys defined by other dynamic link libraries, a DLL must use the globaladdatom function to obtain the identifier of the hotkey.
Fsmodifoers: defines the key that must be pressed together with the key defined by the reset rtkey parameter to generate the wm_hotkey message. This parameter can be a combination of the following values:
Mod_alt: you can press any ALT key. Mod_control: press any ctrl key.
Mod_shift: you can press any shift key.
Mod_win: you can press any windows button. These keys can be recorded in Microsoft Windows logs.
VK: Specifies the virtual key code of the hotkey.

Return Value: if the function is successfully called, a non-O value is returned. If the function call fails, the return value is 0. To obtain more error information, call the getlasterror function.

Note: When a key is terminated, the system searches for the matched key in all hotkeys. Once a matching Hot Key is found, the system will pass the wm_hotkey message to the Message Queue of the thread that registers the hot key. The message is sent to the queue header, so it will be removed in the next message loop. This function cannot associate hotkeys with windows created by other threads.
.
The registerhotkey function fails to be called if the specified key has been defined by other hotkeys.
If the windows marked by the hwnd parameter have registered a hotkey with the same identifier as the ID Parameter definition, the new values of the fsmodifiers and VK parameters will replace the previously defined values of these parameters.
Windows CE: Windows CE 2.0 and later versions support an additional flag for the fsmodifiers parameter. It is called mod_keyup.
If the mod_keyup bit is set, the window will send the wm_hotkey message when the key is pressed or popped up.
Registerhotkey can be used to register hotkeys between threads.

Quick query: Windows NT: 3.1 and later; windows: 95 and later; Windows CE: not supported; header file: winuser. h; library file: user32.lib.

Unregisterhotkey ()
======================================
Function function: This function removes the hot key in the system.

Function prototype: bool unregisterhotkey (hwnd, int ID );

Parameter: the two parameters of this function and regisiterhotkey have the same meaning.

 

; <<
; File name: hotkey1.asm
; Functions: demonstrate registerhotkey () and unregisterhotkey ()
Click "register Hot Key Alt + insert! After successful registration, press
; Alt + insert can open notepad !" Button,
Register Alt + insert as system hotkeys
If the registration fails, an error message is displayed!
If the registration is successful, press Alt + insert,
; Restores the current program window to the foreground
And start the Event Book!
;
Source program and Executable File Download: http://purpleendurer.ys168.com
;
Development Environment: Win 2000 Pro + masm32 V8
;
; Date log
;-------------------------------------------------
; Creation!
; <<

. 386
. Model flat, stdcall
Option Casemap: None
Include/masm32/include/Windows. inc
Include/masm32/include/user32.inc
Include/masm32/include/kernel32.inc
Include/masm32/include/shell32.inc
Includelib/masm32/lib/user32.lib
Includelib/masm32/lib/kernel32.lib
Includelib/masm32/lib/shell32.lib

Winmain proto: DWORD,: DWORD

M_m2m macro D1, D2
Push D2
Pop d1
Endm

. Const
C_buttonid equ 1

. Data
G_szwinclsname DB "demowinclass", 0
G_szappname DB "system hotkey demo program by purpleendurer", 0
G_szbtnclsname DB "button", 0
G_szfailregisterkey DB "no"
G_szregisterkey DB "registration hotkey Alt + insert! After successful registration, press Alt + insert to open notepad! ", 0
G_szfailunregisterkey DB "no"
G_szunregisterkey DB "deregister the hot key Alt + insert! ", 0
G_bregisted DWORD false
G_szopen DB "open", 0
G_sznotepad DB "Notepad", 0

. Data?
Hinstance?
CommandLine lpstr?
G_hwndbutton handle?

. Code
Start:
Invoke getmodulehandle, null
MoV hinstance, eax
Invoke getcommandline
MoV CommandLine, eax
Invoke winmain, hinstance, null, CommandLine, sw_showdefault
Invoke exitprocess, eax

Winmain proc hinst: hinstance, hprevinst: hinstance, using line: lpstr, cmdshow: DWORD
Local WC: wndclassex
Local MSG: msg
Local hwnd: hwnd
MoV WC. cbsize, sizeof wndclassex
MoV WC. style, cs_hredraw or cs_vredraw
MoV WC. lpfnwndproc, offset wndproc
MoV WC. cbclsextra, null
MoV WC. cbwndextra, null
Push hinstance
Pop WC. hinstance
MoV WC. hbrbackground, color_window + 1
MoV WC. lpszmenuname, null
MoV WC. lpszclassname, offset g_szwinclsname
Invoke loadicon, null, idi_application
MoV WC. hicon, eax
MoV WC. hiconsm, eax
Invoke loadcursor, null, idc_arrow
MoV WC. hcursor, eax
Invoke registerclassex, ADDR WC
Invoke createappswex, null, ADDR g_szwinclsname, ADDR g_szappname ,/
Ws_overlappedwindow, cw_usedefault ,/
Cw_usedefault, null, null ,/
Hinst, null
MoV hwnd, eax
Invoke showwindow, hwnd, sw_shownormal
Invoke updatewindow, hwnd
. While true
Invoke getmessage, addr msg, null, 0, 0
. Break. If (! Eax)
Invoke translatemessage, ADDR msg
Invoke dispatchmessage, ADDR msg
. Endw
MoV eax, MSG. wparam
RET
Winmain endp

Wndproc proc hwnd: hwnd, umsg: uint, wparam: wparam, lparam: lparam
. If umsg = wm_destroy
. If g_bregisted = true
; Bool unregisterhotkey (hwnd, int ID );
Invoke unregisterhotkey, hwnd, hwnd
. Endif
Invoke postquitmessage, null
. Elseif umsg = wm_create
; Create button
Invoke createappswex, null, ADDR g_szbtnclsname, ADDR g_szregisterkey ,/
Ws_child or ws_visible or bs_defpushbutton ,/
25, 25,560, 50, hwnd, c_buttonid, hinstance, null
MoV g_hwndbutton, eax

. Elseif umsg = wm_command
MoV eax, wparam
. If lparam! = 0
. If AX = c_buttonid
SHR eax, 16
. If AX = bn_clicked
. If g_bregisted = true
Invoke unregisterhotkey, hwnd, hwnd
. If eax = 0
Invoke MessageBox, hwnd, ADDR g_szfailunregisterkey, ADDR g_szappname, mb_iconerror
RET
. Endif
M_m2m g_bregisted, false
; Modify button text
Invoke setwindowtext, g_hwndbutton, ADDR g_szregisterkey
. Else
; Bool registerhotkey (
; Hwnd, // window to receive hot-key notification
; Int ID, // identifier of Hot Key
; Uint fsmodifiers, // key-modifier flags
; Uint VK // virtual-key code );
; Parameters:
ID is an ID value defined for you. For a thread, the value must be within the range of 0x0000-0xbfff,
For DLL, the value must be within the range of 0xc000-0 xFFFF. The value must be unique in the same process.
; Fsmodifiers indicates the keys used with the hotkey. The value can be:
Mod_alt any ALT key press
Mod_control any control key press
Mod_shift any shift key press
Mod_win press any win key
; VK indicates the virtual key of the Hot Key

Register the hotkey and use the window handle hwnd as the ID
Invoke registerhotkey, hwnd, hwnd, mod_alt, vk_insert
. If eax = 0
Invoke MessageBox, hwnd, ADDR g_szfailregisterkey, ADDR g_szappname, mb_iconerror
RET
. Endif
M_m2m g_bregisted, true
; Modify button text
Invoke setwindowtext, g_hwndbutton, ADDR g_szunregisterkey
. Endif
. Endif
. Endif
. Endif
. Elseif umsg = wm_hotkey
; Registerhotkey () after successful execution, when the hot key (Alt + insert in this program) is pressed,
The program will receive the wm_hotkey message
Message parameters and meanings:
; Idhotkey = (INT) wparam; // identifier of Hot Key
; Fumodifiers = (uint) loword (lparam); // key-modifier flags
; Uvirtkey = (uint) hiword (lparam); // virtual-key code
The three values are the same as the ID when the registerhotkey is called, and the fsmodifiers and VK pairs are
;--------------------------------------------------------
Note: If you have registered multiple hotkeys, You need to determine and handle them here.

; Restore the program window to the foreground
Invoke showwindow, hwnd, sw_restore
Invoke setforegroundwindow, hwnd

; Open notepad
Invoke ShellExecute, hwnd, ADDR g_szopen, ADDR g_sznotepad, null, null, sw_restore
. Else
Invoke defwindowproc, hwnd, umsg, wparam, lparam
RET
. Endif
XOR eax, eax
RET
Wndproc endp

End start

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.