Jump to content

Capture key hold without pressing key


Recommended Posts

Hi,

I'm trying to make it so while you hold A down, a script will run and A won't actually be pressed.
Problem is :
If i use _isPressed to check if A is pressed, it will spam A (because the key is held down)
If i use HotKeySet to capture A, it works but it generates lag.

I want to be able to detect a key hold without this key being pressed in windows.
Is this even possible ?

Example : (hold A or B )
 

#include <misc.au3>

HotKeySet("{ESC}", "Quit")
HotKeySet("b", "go")

$flag = False
$timer = TimerInit()
$timeOld = 0

While 1
    If _IsPressed("41") Then ; A key
        ConsoleWrite("_IsPressed | Timer : " & Int(TimerDiff($timer)) & " | Time since last check : " & Int(TimerDiff($timer) - $timeOld) & @CRLF)
        $timeOld = TimerDiff($timer)
        Sleep(450)
    EndIf
    Sleep(50)
WEnd

Func go()
    If NOT $flag then
        $flag = True
        ConsoleWrite("HotKeySet | Timer : " & Int(TimerDiff($timer)) & " | Time since last check : " & Int(TimerDiff($timer) - $timeOld) & @CRLF)
        $timeOld = TimerDiff($timer)
        Sleep(500)
        $flag = False
    EndIf
EndFunc

Func Quit()
    Exit
EndFunc

 

Link to comment
Share on other sites

I tried BlockInput as well as BlockInputEx UDF, but it actually prevents the key from being pressed without capturing if you press it or not.

I want to block the A key, but still be able to detect a key press/hold.

I don't have a specific use, i just had several scripts where it could be useful - Overlay for instance, where you could only show the overlay while pressing a key, or spamming a key only while you press another key.

Edited by Cotino
Link to comment
Share on other sites

  • Developers
1 hour ago, Cotino said:

I don't have a specific use, i just had several scripts where it could be useful - Overlay for instance, where you could only show the overlay while pressing a key, or spamming a key only while you press another key.

yea ... clear as mud B)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Here's a practical application if you will :

While i hold A, slow the mouse down without actually sending A, and speed the mouse back to normal on release.

Slowing/Speeding the mouse can be done easily with DllCall("user32.dll", "int", "SystemParametersInfo", "int", 0x0071, "int", 0, "int", $speed, "int", 0)

I would use this for a magnifier to get to a precise pixel.

Edited by Cotino
Link to comment
Share on other sites

  • Developers
10 minutes ago, Cotino said:

Here's a practical application if you will

Could be me but I have no idea what that could be useful for...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

If you want to get the pixel color at a specific position with a magnifier but your mouse is too fast (happens to me a lot)

Draw something with more precision on a game like pictionary online or just paint.

Link to comment
Share on other sites

typing "a" (no spam)

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISys.au3>

Local $hDLL = DllOpen("user32.dll")
Local $hWnd = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'))

While 1
    If _IsPressed("41", $hDLL) Then
        _WinAPI_RegisterHotKey($hWnd, 0x011B, 0, 0x41)
        ConsoleWrite("_IsPressed - a Key is pressed (no spam)." & @CRLF)
        ; Wait until key is released.
        While _IsPressed("41", $hDLL)
            Sleep(250)
        WEnd
        _WinAPI_UnregisterHotKey($hWnd, 0x011B)
        ConsoleWrite("_IsPressed - a Key was released." & @CRLF)
    ElseIf _IsPressed("1B", $hDLL) Then
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
        ExitLoop
    EndIf
    Sleep(250)
WEnd

DllClose($hDLL)

 

Edited by Deye
Link to comment
Share on other sites

That works perfectly fine !

I didn't know about _WinAPI_RegisterHotKey, but it does just what i needed.

what's the reason behind 0x011B as ID ? From what i understand the ID can be anything we want ?

Link to comment
Share on other sites

Like a variant

#include <Misc.au3>
#include <WindowsConstants.au3>

HotKeySet("a", "_func")
HotKeySet("{esc}", "_exit")

While Sleep(11)
  If _IsPressed("41") Then
    $t = TimerInit()
    GUICreate("", 200, 200, -1, -1, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    GUISetState()
    While _IsPressed("41")
      Sleep(11)
    WEnd
    GUIDelete()
    ConsoleWrite("Show time: " & Round(TimerDiff($t)) & @CRLF)
  EndIf
WEnd

Func _func()
EndFunc

Func _exit()
  Exit
EndFunc

 

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...