Sign in to follow this  
Followers 0

help with simple script to hold down a key until button is pressed

5 posts in this topic

Posted (edited)

I am trying to write a script which will hold down a keyboard key when the user clicks a button, and when they click another button, the key is released. There is also an exit button

Here is what I have so far. It will start holding the key down when I press the button, due to the While 1 infinite loop, but pressing the button to release the key or exit doesn't work as its stuck in the loop.

How can I modify it so it holds the keydown but lets off when I press the right button ? If I get rid of the While 1 loop, pressing the button only sends 1 instance of the key, it doesn't hold it down like it should.

AutoItSetOption("WinTitleMatchMode", 2)

;#NoTrayIcon


#include <GUIConstants.au3>
#include <Date.au3>

GUICreate("Basic GUI with Buttons", 205,200)

GUICtrlCreateLabel ( "title goes here" , 270, 140, 200, 35 )



$Button6= GUICtrlCreateButton("Control DOWN", 20, 25, 150, 40)
$Button7= GUICtrlCreateButton("Control UP", 20, 65, 150, 40)
$Close= GUICtrlCreateButton("E&xit", 20, 105, 150, 40)
$filemenu = GUICtrlCreateMenu ("Men&u")
$Helpme = GUICtrlCreateMenuItem ( "&Help", $filemenu )
------------------------------------------------------------------------

GUISetBkColor ( 0xFFFFFF )

GuiSetState (1)

While 1
$msg = GUIGetMsg()
Select

Case $msg = $button6

    While 1
        Send("{a down}") ;Holds the A key down  
    WEnd
Case $msg = $button7
    Send("{a up}") 
Keyup()
Case $msg = $Close
Exit
EndSelect

If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend
Edited by Valhalla1

Share this post


Link to post
Share on other sites



Posted (edited)

n

Edited by SparkSoft

Share this post


Link to post
Share on other sites

Posted (edited)

AutoItSetOption("WinTitleMatchMode", 2) 

;#NoTrayIcon 


<GUIConstants.au3>
#include <Date.au3>


GUICreate("Basic GUI with Buttons", 205,200) 

GUICtrlCreateLabel ( "title goes here" , 270, 140, 200, 35 ) 



$Button6= GUICtrlCreateButton("Control DOWN", 20, 25, 150, 40) 
$Button7= GUICtrlCreateButton("Control UP", 20, 65, 150, 40) 
$Close= GUICtrlCreateButton("E&xit", 20, 105, 150, 40) 
$filemenu = GUICtrlCreateMenu ("Men&u") 
$Helpme = GUICtrlCreateMenuItem ( "&Help", $filemenu ) 
------------------------------------------------------------------------ 

GUISetBkColor ( 0xFFFFFF ) 

GuiSetState (1) 

While 1 
$msg = GUIGetMsg() 
Select 

Case $msg = $button6 
do

Send("{a down}") ;Holds the A key down 

         until $msg = $button6 
Case $msg = $button7 
do 
Send("{a up}") 
until $msg = $button7 
Keyup() 
Case $msg = $Close 
Exit 
EndSelect 

If $msg = $GUI_EVENT_CLOSE Then ExitLoop 
Wend

Here ive put in a do loop, if the button is pressed again it should stop sending a up or a down

I dont know if this will work because I dont know what you want to do with the script,

Edited by SparkSoft

Share this post


Link to post
Share on other sites

Posted

thanks for the reply. That looked like it should work when I saw it, but when I tried it it seems to only hit the key once, not hold it down. The same thing happens if I remove the loop entirely. Curious, the Send("{a down}") command only seems to truly hold down the button if it is in a While 1 infinite loop

here is the cleaned up code using your example, which doesn't truly hold down the a button like it should

AutoItSetOption("WinTitleMatchMode", 2) 

;#NoTrayIcon 


#include <GUIConstants.au3>
#include <Date.au3>


GUICreate("Basic GUI with Buttons", 205,200) 

GUICtrlCreateLabel ( "title goes here" , 270, 140, 200, 35 ) 



$Button6= GUICtrlCreateButton("Control DOWN", 20, 25, 150, 40) 
$Button7= GUICtrlCreateButton("Control UP", 20, 65, 150, 40) 
$Close= GUICtrlCreateButton("E&xit", 20, 105, 150, 40) 
$filemenu = GUICtrlCreateMenu ("Men&u") 
$Helpme = GUICtrlCreateMenuItem ( "&Help", $filemenu ) 
;------------------------------------------------------------------------ 

GUISetBkColor ( 0xFFFFFF ) 

GuiSetState (1) 

While 1 
$msg = GUIGetMsg() 
Select 

Case $msg = $button6 
    do  
        Send("{a down}") ;Holds the A key down 
        
         until $msg = $button6 
Case $msg = $button7 
    do 
Send("{a up}") 
until $msg = $button7 
    Send("{a up}")
Case $msg = $Close 
Exit 
EndSelect 

If $msg = $GUI_EVENT_CLOSE Then ExitLoop 
Wend

Share this post


Link to post
Share on other sites

Posted (edited)

you could use the a button with ShellExecute() command to run another script

then use another button with ProcessClose() to stop it, but the script has to be compiled before you run it

this is one way to stop it going into an infinite loop

So it would go like this:

Case $Button1
ShellExecute(@ScriptDir "\send a.exe" ;this is the script you wanna put a loop in and compile it
Case $Button2
ProcessClose("send a.exe")

and this would be the one you would need to compile and have in the same folder

While 1
Send("a")
Wend

EDIT: The reason my script doesnt work is because while (no pun intended :graduated:) the script is in while loop it isnt listening for gui messages (Sorry 4 that)

Edited by SparkSoft

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  
Followers 0