Jump to content
Sign in to follow this  
Cotino

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

 

Share this post


Link to post
Share on other sites

Why, what exactly do you want to accomplish? Am i understanding you correctly - you want to block the 'A' key?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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.
  :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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.
  :)

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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 ?

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites
4 hours ago, Cotino said:

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

Game Automation is not allowed here.


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...