Jump to content

Hotkeyset () call only once


Recommended Posts

Is there any way for function to be called only once when a hotkey is pressed without having to use _WinAPI_GetAsyncKeyState or _IsPressed to know when the key has been released?

Link to comment
Share on other sites

  • Moderators

Belini,

Unset the HotKey when entering the function:

#include <MsgBoxConstants.au3>

HotKeySet("^t", "_Func")
HotKeySet("{ESC}", "_Exit")

While 1
    Sleep(100)
WEnd

Func _Func()
    HotKeySet("^t")
    MsgBox($MB_SYSTEMMODAL, "HotKey", "Only going to work once!")
EndFunc

Func _Exit()
    Exit
EndFunc

M23

 

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

Sorry @ Melba23 I was not clear, I want to call the function only when the hotkey is no longer pressed but I do not want to disable the hotkey, I will use this function for counting pulses and can not count more than once for as long as the key was pressed.

Link to comment
Share on other sites

Link to comment
Share on other sites

@Nine are very short pulses and with small interval between them so I can not use _IsPressed or _WinAPI_GetAsyncKeyState to know when the key was released, what I want is to use hotkeyset () but only count one pulse for as long as the key was pressed, I searched the hotkeyset () function documentation to see if I could make changes to it but could not find it.

EDITED:  when I use _IsPressed or _WinAPI_GetAsyncKeyState some pulses are not counted and when I use only hotkeyset I get all the pulses but when the key happens to get stuck it will count infinitely because hotkeyset calls the function all the time the key is pressed.

Edited by Belini
Link to comment
Share on other sites

Are your reaction times quicker than the delay in that loop? Seriously doubt it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH as incredible as it seems I can not get all pulses using loop because they are very fast and the interval between them is also very small, I can only get all pulses using hotkeyset but if the key is stuck the count does not stop so I need to call the function only once until the button is released but only using hotkeyset.

@Nine deactivating hotkey and using _Ispressed I had already tried and for this I mentioned in the first post I need to do without using _IsPressed or _WinAPI_GetAsyncKeyState

Link to comment
Share on other sites

Link to comment
Share on other sites

you're right, these wrists are generated by a note acceptor that sends out wrists when a ballot is placed.

Link to comment
Share on other sites

2 hours ago, Belini said:

you're right, these wrists are generated by a note acceptor that sends out wrists when a ballot is placed.

English is not my first language, but that seems to me like Egyptian.  And I know nothing about it.  Take a look at a global callback, I hardly can think of a faster method.

Link to comment
Share on other sites

Define pulse, and wrists.

Is a pulse a send?  I'm not understanding if it's a programmatic send, or user based send.  There is no way a human can send quicker than the 10 ms sleeps....that's 100 individual presses a second.

 

You can remove all sleeps, but watch one of your cpu cores go up to 100%

#include <Misc.au3>

HotKeySet("{ESC}", "_Exit")

$sendCount = 0
While 1
    If _IsPressed("70") Then
        $sendCount += 1
        While _IsPressed ("70")
        WEnd
        ContinueLoop
    EndIf
    Sleep(10)
WEnd



Func _Exit()
    MsgBox(1,1,$sendCount)
    Exit
EndFunc

if that's not fast enough, then AutoIT is not the solution you are looking for...slightly modified to include sleeps when the current loop did not have _ispressed.

Proof of concept...I compiled and started the above, and then ran:

AutoItSetOption("SendKeyDelay",5)

For $i = 1 To 40
    AutoItSetOption("SendKeyDownDelay",Random(10,100,1))
    Send("{F1}")
Next
Send("{ESC}")

The message box displayed 40

 

Just for fun, I did this one to see how fast I can send '0' repeatedly:

 

HotKeySet("{ESC}", "_Exit")
$j = TimerInit()
$sendCount = 0
While 1
    If _IsPressed("30") Then
        $i = TimerInit()
        $j = TimerDiff($j)
        While _IsPressed ("30")
        WEnd
        ConsoleWrite(TimerDiff($i) & @TAB & $j & @CRLF)
        $j = TimerInit()
        ContinueLoop
    EndIf
    Sleep(10)
WEnd


Func _Exit()
    Exit
EndFunc

output: TimeKeyIsDown|TimeWaitForNextSend

38.5811 1433.9669
60.3951 75.5055
57.3372 63.8362
19.618  96.3084
35.7309 85.2414
10.7798 85.2756
38.7877 51.8923
42.2014 73.8966
41.3293 64.4828
29.7217 86.5125
37.8254 64.5799
55.5981 63.7196
52.7291 64.0097
52.5025 62.7217
38.963  77.1272
58.137  62.738
49.7552 76.4725
68.762  63.2584
55.5568 64.6286
36.5252 84.6798
75.4104 75.4531
53.4436 52.8638
44.8142 86.4005
61.8499 84.044
57.119  74.213
40.9023 95.4116
67.0561 84.0198
59.6137 61.6889
30.9173 95.3387
51.887  94.1365
42.998  63.1183
9.6173  116.8636
30.0914 85.7146
22.4921 93.7678
47.7521 84.4666
43.1216 61.6884
29.9012 86.1493
36.3007 84.6724
25.0176 86.1363
40.8244 84.9485
35.4981 85.6375
26.3855 84.7178
27.4497 73.2501
5.444   95.4684

humans are pretty slow...I was able to see 2 sends less than 10 MS , but that is an like in the 3 sigma range. I'd be lucky if I got 11 sends a second.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

@jdelaney I'm traveling now and when I go back I'll do the tests, thanks for the tips.

Link to comment
Share on other sites

I got it using DllCall, thanks to everyone who gave tips.

#include <WinAPI.au3>

While 1
    Key_IsPressed()
    GUIGetMsg()
WEnd

Func Key_IsPressed()
    local Static $count = 0, $state = 0
    Local $Ret = DllCall('user32.dll', 'short', 'GetAsyncKeyState', 'int', 111); Checks if the NUMPADDIV key has been pressed
    If BitAND($Ret[0], 0x8000) > 0 And $state = 0 Then
        $count += 1
        ConsoleWrite("Key pressed " & $count & @CRLF)
        $state = 1
    Else
        If BitAND($Ret[0], 0x8000) = 0 And $state = 1 Then
            $state = 0
        EndIf
    EndIf
EndFunc   ;==>Key_IsPressed

 

Link to comment
Share on other sites

I thought you didn't want to use _IsPressed? Your code just does exactly what _IsPressed does.

Func _IsPressed($sHexKey, $vDLL = "user32.dll")
    Local $aReturn = DllCall($vDLL, "short", "GetAsyncKeyState", "int", "0x" & $sHexKey)
    If @error Then Return SetError(@error, @extended, False)
    Return BitAND($aReturn[0], 0x8000) <> 0
EndFunc   ;==>_IsPressed

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Link to comment
Share on other sites

39 minutes ago, Belini said:

GUIGetMsg() helped reduce key scanning time

Yeah, and a Sleep(10) would have done the same thing. You're just recreating or misusing functions because you're not paying attention to what you're doing and not reading the help file.

So far in that script, you misused GUIGetMsg for no reason other than simply to insert a sleep into the script, where you could have used Sleep to do it. Then you recreated a function you said you wouldn't use, which doesn't make sense.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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