Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

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


  • Please log in to reply
4 replies to this topic

#1 Valhalla1

Valhalla1

    Seeker

  • Active Members
  • 25 posts

Posted 29 November 2010 - 11:21 PM

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.

AutoIt         
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, 29 November 2010 - 11:24 PM.








#2 SparkSoft

SparkSoft

    Wayfarer

  • Active Members
  • Pip
  • 98 posts

Posted 30 November 2010 - 10:35 AM

n

Edited by SparkSoft, 30 November 2010 - 10:41 AM.

First Ever Script/App

Simple Battery Meter


#3 SparkSoft

SparkSoft

    Wayfarer

  • Active Members
  • Pip
  • 98 posts

Posted 30 November 2010 - 10:40 AM

AutoIt         
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, 30 November 2010 - 10:48 AM.

First Ever Script/App

Simple Battery Meter


#4 Valhalla1

Valhalla1

    Seeker

  • Active Members
  • 25 posts

Posted 30 November 2010 - 03:02 PM

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

AutoIt         
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


#5 SparkSoft

SparkSoft

    Wayfarer

  • Active Members
  • Pip
  • 98 posts

Posted 01 December 2010 - 12:05 AM

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, 01 December 2010 - 12:13 AM.

First Ever Script/App

Simple Battery Meter





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users