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

How do you have a script run in the background?


  • Please log in to reply
11 replies to this topic

#1 autoitman1

autoitman1

    Seeker

  • Active Members
  • 39 posts

Posted 30 June 2011 - 02:07 PM

I have a program that I use a bunch of HotKeyPress() functions for to map every key on the board to make a sound when pressed. But it interferes with regular programs, like typing on word or notepad. Is there a way to set it in the background?

Thanks,
:)







#2 sleepydvdr

sleepydvdr

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 572 posts

Posted 30 June 2011 - 02:10 PM

Yes there is a way. Do not create a Gui interface and make this be the first line in your script:
#NoTrayIcon

#include <ByteMe.au3>

#3 autoitman1

autoitman1

    Seeker

  • Active Members
  • 39 posts

Posted 30 June 2011 - 02:17 PM

interesting, after I add that, can i add a hotkey that brings up a GUI for setting changes and then when save is pressed the GUI will close and the Script will once again run int the background?

Thank you

#4 Tvern

Tvern

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 972 posts

Posted 30 June 2011 - 02:54 PM

I assume you mean that the hotkeys are not being send to whatever program you are working in. Two options to overcome this are:
1. Instead of HotKeySet, use _IsPressed in a loop. To avoid spamming the function, make it wait until the key is released, before returning. (There are other options to avoid the spamming if you want to function to return right away.)
2. At the start of a function, disable the HotKeySet that called it, then use Send() to send the keys and set the hotkey again.

#5 autoitman1

autoitman1

    Seeker

  • Active Members
  • 39 posts

Posted 30 June 2011 - 03:08 PM

here is the program...but i don't understand what you mean with the send(), unless you mean write a function for every key...which would be tedious
#NoTrayIcon HotKeySet("{Esc}", "stopscript") HotKeySet("{A}", "beeper") HotKeySet("{a}", "beeper") Func stopscript()     Exit EndFunc   ;==>stopscript Func beeper()     Beep(2000, 500) EndFunc   ;==>beeper $x = True While 1 WEnd


Basically, after i runt the script, i just want to be able to continue with my regular computer activities..but everytime i press "A" or "a", the beep should occur
Instead...the script takes over and the only way to send the keypress to other programs, like word or office, is to exit out of the script

#6 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 10,597 posts

Posted 30 June 2011 - 03:22 PM

Yes, you would need to use Send for every key that you have a hotkey set for, otherwise the hotkey is capturing all keystrokes and sending them to your script.

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 an XP machine, 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!

 

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.
GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips. Latin Square password generator

 

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.


#7 autoitman1

autoitman1

    Seeker

  • Active Members
  • 39 posts

Posted 30 June 2011 - 03:26 PM

hmm damn, i guess i have to look into the _ispressed thing then...time to learn how to use DLL's lol

#8 autoitman1

autoitman1

    Seeker

  • Active Members
  • 39 posts

Posted 30 June 2011 - 03:34 PM

I found this in the example script section :), credits to the guy who wrote it of course
question is...how does the _IsPressed function work? and why is the length of the hexidecimal ? and why does he put $i from 1 to 121?
#NoTrayIcon #include <Misc.au3> $dll = DllOpen("user32.dll") While 1     for $i=1 to 221         If _IsPressed(Hex($i, 2), $dll) Then             beep(random(100,1000,1), 100)             ExitLoop         EndIf     Next     Sleep ( 100 ) WEnd DllClose($dll)


I'm still very new to programming...obviously lol...

thank you

#9 Tvern

Tvern

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 972 posts

Posted 30 June 2011 - 03:56 PM

I posted something like this a while ago, so I edited it to fit your script.
_IspressedEx is a modified version of _IsPressed, that only calls a function once for each keypress, much like HotKeySet. You can replace those lines with _IsPressed() if you want to see the difference.
_IsPressed and _IsPressedEx both use a hex code to determine what key is pressed. (values can be found in the helpfile for _IsPressed). The script you posted just checks pretty much every key and would result in a whole lot of beeping.
You don't need to know much about DLL's to use these functions. Just open it before the function is called and close it when you exit. _IsPressed will even open and close the DLL automatically if you don't supply a handle, but that slows things down, so I left it out of my function.

AutoIt         
#NoTrayIcon Global $aKeyStillPressed[255] ;used to keep track of keystates Global $dll = DllOpen("user32.dll") ;Open the dll used in _IsPressedEx While 1     If _IsPressedEx('1b', $dll) Then stopscript() ;These lines replace the hotkeyset and have to be in a the main loop     If _IsPressedEx('41', $dll) Then beeper() ;Captures both a and A. Check states of capslock and shipt if you need to know what was pressed.     Sleep(10) ;important to avoid cpu usage! WEnd Func stopscript()     DllClose($dll) ;Tidy up after yourself!     Exit EndFunc Func beeper()     Beep(2000, 500) EndFunc Func _IsPressedEx($hexKey, $u32dll)     Local $iKey = Dec($hexKey)     If $iKey > 255 Or $iKey < 0 Then Return SetError(1,0,0)     Local $aR = DllCall($u32dll, "int", "GetAsyncKeyState", "int",'0x' & $hexKey)     If @error Then Return SetError(2,0,0)     If $aKeyStillPressed[$iKey] Then ;If the key was already registered as pressed         If BitAND($aR[0], 0x8000) <> 0x8000 Then $aKeyStillPressed[$iKey] = False ;Check if it is still pressed and update if needed         Return 0 ;do nothing     ElseIf BitAND($aR[0], 0x8000) = 0x8000 Then ;If the key wasn't registered as pressed, but is pressed now it must be a new keypress         $aKeyStillPressed[$iKey] = True ;Update the pressed status         Return 1     EndIf     Return 0 EndFunc


#10 autoitman1

autoitman1

    Seeker

  • Active Members
  • 39 posts

Posted 30 June 2011 - 04:07 PM

awesome! I will play around with that, thanks :)

just a quick learning question though...
where are you entering true or false values for the $aKeyStillPressed[255] array?

i can see it checks for a true or false here
 If $aKeyStillPressed[$iKey] Then ;If the key was already registered as pressed

but i cannot see where the array is modified in anyway

thank you

#11 Tvern

Tvern

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 972 posts

Posted 30 June 2011 - 05:40 PM

;Line 26: If BitAND($aR[0], 0x8000) <> 0x8000 Then $aKeyStillPressed[$iKey] = False ;Check if it is still pressed and update if needed ;and line 29: $aKeyStillPressed[$iKey] = True ;Update the pressed status

You don't really need to do anything with the array though, unless you want to set timer that allows the key to start repeating if it's pressed for long enough.

#12 autoitman1

autoitman1

    Seeker

  • Active Members
  • 39 posts

Posted 30 June 2011 - 07:11 PM

ah i see, thanks man...i learned a good bit from that code




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users