Jump to content

"Re-Enable" a key


Go to solution Solved by Melba23,

Recommended Posts

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 by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Ehm...not

 

I forgot to say why :sweating:

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 by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Moderators
  • Solution

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:

#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)
EndFunc
All clear? :)

 

M23

Edited by Melba23
Added clean up code

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Moderators

Terenz,

I would do it like this:

#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? :huh:

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...