Here's another example of a Win32lib program that was originally posted on the Euphoria mailing list by Eddy Van Esch. It illustrates how you can use the RegisterHotKey() function, and create an event handler to use these user-defined keys.
When this program first starts up, it has a button which hides the window. The only way to restore the window's visibility is through the user-defined hot key combination [CTRL] + [S].
The window's color can also be changed by another two pairs of hot keys.
The [CTRL] + [Z] combination changes the color to Yellow,
and the [CTRL] + [X] combination changes the color to Gray.
This happens whether the window is visible or not.
Eddy had originally used the UnregisterHotKey() function in the program's closing routine, but this function really only needs to be called if you want to change the definition of key combinations dynamically within your program.
Note that I'm using the id of the registered hot key to change the color back to Gray, whereas the other hot key events are triggered by the virtual key values. This would allow you to re-define the value of this hot key combination by using the UnregisterHotKey() / RegisterHotKey() functions, and your event handler would still act appropriately, since it's still looking for an id of (3).
Executing a simple code snippet like the one below, for example, would allow the event handler to change the window to Gray when the user pressed the [CTRL] + [G] key combination.
..end of lesson.
-- hotkey2.exw
-- demo of hiding a window, and getting it back,
-- or changing colors, by pressing a hotkey combination
-- originally by Eddy Van Esch, 15/01/2001
include win32lib.ew
without warning
constant
zWM_HOTKEY = #312,
zMOD_CTRL = #2,
key1 = 'S', -- hotkeys are CTRL S,Z,X... change as necessary
key2 = 'Z',
key3 = 'X',
zRegisterHotKey = registerw32Function( user32,
"RegisterHotKey",{ C_LONG, C_LONG, C_LONG, C_LONG }, C_LONG ),
zUnregisterHotKey = registerw32Function( user32,
"UnregisterHotKey",{ C_LONG, C_LONG }, C_LONG )
atom ok, myHandle
sequence msg
msg="Click 'Hide window' button to hide this window.\n" &
"Press hotkey combination CTRL S to show the window again.\n\n" &
"CTRL Z, and CTRL X change the window's colors."
constant
Win1 = create( Window, "HotKey Demo", 0, Default, Default,400, 200, 0 ),
HideBtn = create( PushButton, "Hide window", Win1,120, 12, 144, 40, 0 ),
TheTxt = create( LText, msg, Win1, 40, 70, 300, 70, 0 )
-- Hide button's behavior
procedure onClick_HideBtn()
setVisible( Win1, False ) -- Hide the window
end procedure
-- Event handler: intercept the hotkey and define what to do with it:
procedure Events(atom iMsg, atom wparam, atom lparam)
if iMsg = zWM_HOTKEY then
if hi_word(lparam) = 'S' then
--unhide window, can be used to do anything you
--want here...
setVisible( Win1, True )
elsif hi_word(lparam) = 'Z' then
setWindowBackColor(Win1,Yellow)
setWindowBackColor(TheTxt,Yellow)
-- elsif hi_word(lparam) = 'X' then -- not used
elsif wparam = 3 then -- using id instead of virtual key value
setWindowBackColor(Win1,getSysColor(COLOR_BTNFACE))
setWindowBackColor(TheTxt,getSysColor(COLOR_BTNFACE))
end if
end if
end procedure
procedure register_keys()
-- get our window's handle
myHandle = getHandle(Win1)
-- register hotkeys with id's of 1, 2, 3
ok = w32Func(zRegisterHotKey, {myHandle, 1, zMOD_CTRL, key1})
ok = w32Func(zRegisterHotKey, {myHandle, 2, zMOD_CTRL, key2})
ok = w32Func(zRegisterHotKey, {myHandle, 3, zMOD_CTRL, key3})
end procedure
--
onClick[HideBtn] = routine_id( "onClick_HideBtn" )
onEvent[Win1] = routine_id( "Events" )
onOpen[Win1] = routine_id("register_keys")
WinMain( Win1, Normal ) --normal event loop
--------
constant key4 = 'G'
ok = w32Func(zUnregisterHotKey, {myHandle, 3})
ok = w32Func(zRegisterHotKey, {myHandle, 3, zMOD_CTRL, key4})