Terenz Posted July 27, 2013 Share Posted July 27, 2013 (edited) Hi guys, I have searched arond but i don't have found any solution I want to lock three keys, F1-F2-F3 for a external application, i have do this: #include <WinAPI.au3> HotKeySet("{ESC}", "Terminate") Global $pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr") Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _WinAPI_GetModuleHandle(0), 0) While 1 Sleep(100) WEnd Func _KeyProc($nCode, $wParam, $lParam) If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam) Local $vkCode = DllStructGetData($KBDLLHOOKSTRUCT, "vkCode") If $vkCode = 0x70 Then Return 1 If $vkCode = 0x71 Then Return 1 If $vkCode = 0x72 Then Return 1 _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) EndFunc Func Terminate() DllCallbackFree($pStub_KeyProc) _WinAPI_UnhookWindowsHookEx($hHook) EndFunc But if i want to reactivate one of those key ( example F1 ) but always stop the F2-F3...how i can do it? Thanks Edited July 27, 2013 by Terenz Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
mikell Posted July 27, 2013 Share Posted July 27, 2013 Is not it possible to do this using Hotkeyset ? much easier to manage... HotKeySet("{F3}", "test") While 1 Sleep(100) WEnd Func test() Sleep(1) EndFunc Link to comment Share on other sites More sharing options...
Terenz Posted July 27, 2013 Author Share Posted July 27, 2013 Ehm...not Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
Terenz Posted July 27, 2013 Author Share Posted July 27, 2013 (edited) Ehm...not I forgot to say why I have try to use HotKeySet but they will ingnored, with low-level hook work the "disable" but i need also to re-enable one-two or all three keys, not only disable. But on the forum is write only how to enable ALL the keys ( DllCallbackFree / _WinAPI_UnhookWindowsHookEx ), not how to re-enable only one or two Edited July 27, 2013 by Terenz Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
Moderators Solution Melba23 Posted July 27, 2013 Moderators Solution Share Posted July 27, 2013 (edited) Terenz,Why not use some Global variables to determine whether the keys are enabled or not? This shows how it works for the F1 key - you just need to add similar code for the others:expandcollapse popup#include <GUIConstantsEx.au3> #include <Constants.au3> #include <WinAPI.au3> Global $fF1 = False Global $pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr") Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _WinAPI_GetModuleHandle(0), 0) $hGUI = GUICreate("Test", 500, 500) $cCheck_F1 = GUICtrlCreateCheckbox(" Disable F1", 10, 10, 200, 20) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE DllCallbackFree($pStub_KeyProc) _WinAPI_UnhookWindowsHookEx($hHook) Exit Case $cCheck_F1 If BitAND(GUICtrlRead($cCheck_F1), $GUI_CHECKED) Then $fF1 = True GUICtrlSetData($cCheck_F1, " F1 Disabled") Else $fF1 = False GUICtrlSetData($cCheck_F1, " Disable F1") EndIf EndSwitch WEnd Func _KeyProc($nCode, $wParam, $lParam) If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam) Local $vkCode = DllStructGetData($KBDLLHOOKSTRUCT, "vkCode") Switch $vkCode Case 0x70 If $fF1 Then ConsoleWrite("F1 intercepted" & @CRLF) Return 1 Else ConsoleWrite("F1 allowed" & @CRLF) EndIf EndSwitch _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) EndFuncAll clear? M23 Edited July 31, 2013 by Melba23 Added clean up code Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Terenz Posted July 27, 2013 Author Share Posted July 27, 2013 (edited) Hi Melba, as always nice example. I want to make you my last question. If i want to allow the "press" of the F1, example every 10 second, in this way: Press F1, it work --> now disable F1 for 10 seconds, if you press don't do nothing --> After 10 sec, F1 now work again, i'll click it --> disable F1 for 10 seconds etc. I want to avoid Do...Until because Autoit is not multi-threading, so i can't do that for two different key. This is what i have done: Global $Timer While 1 If _IsPressed("70", $hDLL) And Not $fF1 Then ; 70 = F1 $Timer = TimerInit() $fF1 = True EndIf If $fF1 = True Then If TimerDiff($Timer) >= 10000 Then $fF1 = False EndIf EndIf Sleep(100) WEnd Seems not work as expected, you know why? Thanks again Edited July 27, 2013 by Terenz Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 27, 2013 Moderators Share Posted July 27, 2013 (edited) Terenz, I would do it like this: expandcollapse popup#include <GUIConstantsEx.au3> #include <Constants.au3> #include <WinAPI.au3> Global $nTimer_F1 = 0 Global $pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr") Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($pStub_KeyProc), _WinAPI_GetModuleHandle(0), 0) $hGUI = GUICreate("Test", 500, 500) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch If $nTimer_F1 Then If TimerDiff($nTimer_F1) > 10000 Then ConsoleWrite("F1 re-enabled at: " & @SEC & @CRLF) $nTimer_F1 = 0 EndIf EndIf WEnd Func _KeyProc($nCode, $wParam, $lParam) If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) Local $KBDLLHOOKSTRUCT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo", $lParam) Local $vkCode = DllStructGetData($KBDLLHOOKSTRUCT, "vkCode") Switch $vkCode Case 0x70 If Not $nTimer_F1 Then ConsoleWrite("F1 allowed through" & @CRLF) ConsoleWrite("F1 disabled at: " & @SEC & @CRLF) $nTimer_F1 = TimerInit() Else ConsoleWrite("F1 intercepted" & @CRLF) Return 1 EndIf EndSwitch _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) EndFunc All clear? M23 Edit: And why on earth do you need this functionality? Edited July 27, 2013 by Melba23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Terenz Posted July 27, 2013 Author Share Posted July 27, 2013 You are abosolutely my Hero of the Week Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now