Jump to content

How do you have a script run in the background?


Recommended Posts

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,

:)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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.

#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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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