Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
keypress detect (key translated in VK an readable format)
#1
I am now trying to create macros where the user can trigger QM executables using registered hotkeys.
The problem I have is that I need to save the users preferred hotkey in a textfile.

I figured out to do some basic transformations/translations:

Macro Macro11
Code:
Copy      Help
str s="T"
__QmKeysToText s &_s
out _s

str a
a="F10"
int vk
QmKeyCodeToVK(a &vk)
out vk

But what I want is a simple dialogbox with 2 inputfields.
As soon the user presses any key or key combination (CTRL, SHIFT, ALT,... + KEY), the dialogbox updates the 2 inputfields.
One inputfield shows the key pressed in human readable format.
The other inputfield shows the key translated in VK key codes (this VK code I am saving in ini files)

I made a start here, but what's an efficient way to implement this?
The problem is that I would use a lot of 'ifk' commands, I think this is not needed.

Function keypress_detect
Code:
Copy      Help
;\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages


;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 222 76 "Dialog"
;1 Static 0x54000200 0x0 8 24 104 12 "Pressed key (human readable):"
;2 Edit 0x54030080 0x200 8 40 104 12 ""
;3 Static 0x54000200 0x0 128 24 30 12 "VK code:"
;4 Edit 0x54030080 0x200 128 40 78 12 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2040105 "*" "" "" ""

ret
;messages
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1
#2
One way - use hotkey control in dialog, like in QM Options dialog, Hotkeys tab. But the control does not support the Windows key and some others (Delete, Backspace...).
Other - use keyboard hook.

Example with hook. Need QM 2.4.1.4+.
Function dialog_hotkey_control_with_hook
Code:
Copy      Help
\Dialog_Editor

str controls = "3"
str e3
if(!ShowDialog("" &sub.DlgProc &controls)) ret

;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "Dialog"
;3 Edit 0x54030080 0x200 64 8 152 12 ""
;4 Static 0x54000200 0x0 8 8 48 12 "Hotkey"
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040105 "*" "" "" ""

#sub DlgProc
function# hDlg message wParam lParam

int- t_hhk t_hk
sel message
,case WM_INITDIALOG
,t_hhk=id(3 hDlg)
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case EN_SETFOCUS<<16|3
,int-- t_hh
,t_hh=SetWindowsHookEx(WH_KEYBOARD &sub.Hook_WH_KEYBOARD _hinst GetCurrentThreadId)
,case EN_KILLFOCUS<<16|3
,UnhookWindowsHookEx t_hh
,case IDOK
,out "hotkey: vk=%i, mod=%i" t_hk&255 t_hk>>8
,;note: mod used with RegisterHotKey is different: need to swap the first and third bits.
,case IDCANCEL
ret 1


#sub Hook_WH_KEYBOARD
function# nCode vk lparam
if(nCode<0) goto gNext

int up(lparam&0x80000000) m mod

sel(vk) case VK_SHIFT m=1; case VK_CONTROL m=2; case VK_MENU m=4; case [VK_LWIN,VK_RWIN] m=8
int-- t_mod
if(m) vk=0; if(up) t_mod~m; else t_mod|m
else mod=t_mod

if(!up) sub.SetHotkey vk mod

ret 1

;gNext
ret CallNextHookEx(0 nCode vk +lparam)


#sub SetHotkey
function vk mod

int- t_hhk t_hk
FormatKeyString vk mod &_s
_s.setwintext(t_hhk)
t_hk=mod<<8|vk
#3
Ok thanks!

edit:
WOW!
thank you for that example!!!


Forum Jump:


Users browsing this thread: 2 Guest(s)