Jump to content

Weird behaviour: Func turns into a loop


leuce
 Share

Recommended Posts

G'day everyone

I'm trying to create a script that will send "delete and space" when I press the spacebar, if I have pressed Shift+arrow withing the second before I press the spacebar. However, the script malfunctions in a way that I can't figure out what's wrong.

#include <Misc.au3>

$dll = DllOpen("user32.dll")

While 1
Sleep ("100")
If _IsPressed("27", $dll) Then ; if right arrow is pressed
If _IsPressed("10", $dll) Then ; if shift is pressed
HotKeySet ("{SPACE}", "spacedelete") ; then make spacebar a hotkey
EndIf
EndIf
WEnd

Func spacedelete()
Send ("{DELETE}")
Send ("{SPACE}")
HotKeySet ("{SPACE}") ; make spacebar a normal spacebar again
EndIf
EndFunc

DllClose($dll)

I've tested it by adding "MsgBox"es in various places in the script to see what happens, and the While...WEnd operation performs successfully (i.e. the script successfully knows when I have pressed Shift+arrow), but when I then press the spacebar, the Function is repeated over and over and over (even if I'm not holding in the spacebar), in effect deleting everything in front of the cursor (unless I kill the script).

I tried to fix it, like this:

#include <Misc.au3>

Global $fixer

$dll = DllOpen("user32.dll")

While 1
Sleep ("100")
If _IsPressed("27", $dll) Then ; right arrow
If _IsPressed("10", $dll) Then ; shift
HotKeySet ("{SPACE}", "spacedelete")
$fixer = 1
EndIf
EndIf
WEnd

Func spacedelete()
If $fixer = 1 Then
Send ("{DELETE}")
Send ("{SPACE}")
HotKeySet ("{SPACE}")
$fixer = 2
EndIf
EndFunc

DllClose($dll)

...but to no avail.

Does anyone know why my Func turns into a loop? Does anyone know of a way to stop it from being a loop (my $fixer solution has no effect on it)?

Thanks

Samuel

Link to comment
Share on other sites

  • Developers

try:

Func spacedelete()
    If $fixer = 1 Then
        HotKeySet("{SPACE}")
        Send("{DELETE}")
        Send("{SPACE}")
        $fixer = 2
    EndIf
EndFunc   ;==>spacedelete
Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

G'day everyone

I'm trying to create a script that will send "delete and space" when I press the spacebar, if I have pressed Shift+arrow withing the second before I press the spacebar. However, the script malfunctions in a way that I can't figure out what's wrong.

#include <Misc.au3>

$dll = DllOpen("user32.dll")

While 1
Sleep ("100")
If _IsPressed("27", $dll) Then ; if right arrow is pressed
If _IsPressed("10", $dll) Then ; if shift is pressed
HotKeySet ("{SPACE}", "spacedelete") ; then make spacebar a hotkey
EndIf
EndIf
WEnd

Func spacedelete()
Send ("{DELETE}")
Send ("{SPACE}")
HotKeySet ("{SPACE}") ; make spacebar a normal spacebar again
EndIf
EndFunc

DllClose($dll)

I've tested it by adding "MsgBox"es in various places in the script to see what happens, and the While...WEnd operation performs successfully (i.e. the script successfully knows when I have pressed Shift+arrow), but when I then press the spacebar, the Function is repeated over and over and over (even if I'm not holding in the spacebar), in effect deleting everything in front of the cursor (unless I kill the script).

I tried to fix it, like this:

#include <Misc.au3>

Global $fixer

$dll = DllOpen("user32.dll")

While 1
Sleep ("100")
If _IsPressed("27", $dll) Then ; right arrow
If _IsPressed("10", $dll) Then ; shift
HotKeySet ("{SPACE}", "spacedelete")
$fixer = 1
EndIf
EndIf
WEnd

Func spacedelete()
If $fixer = 1 Then
Send ("{DELETE}")
Send ("{SPACE}")
HotKeySet ("{SPACE}")
$fixer = 2
EndIf
EndFunc

DllClose($dll)

...but to no avail.

Does anyone know why my Func turns into a loop? Does anyone know of a way to stop it from being a loop (my $fixer solution has no effect on it)?

Thanks

Samuel

After setting the hotkey, pressing space activates the spacedelete(), which pressesn space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() ...... :graduated:

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

After setting the hotkey, pressing space activates the spacedelete(), which pressesn space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() which presses space which activates the spacedelete() ...... :graduated:

I knew it must be something simple. Changing it to this works nicely:

Func spacedelete()
If $fixer = 1 Then
HotKeySet ("{SPACE}")
Send ("{DELETE}")
Send ("{SPACE}")
$fixer = 2
EndIf
EndFunc

Thanks for pointing that out. :-)

Samuel

Link to comment
Share on other sites

try:

Func spacedelete()
    If $fixer = 1 Then
        HotKeySet("{SPACE}")
        Send("{DELETE}")
        Send("{SPACE}")
        $fixer = 2
    EndIf
EndFunc   ;==>spacedelete

Thanks, Jos. When I saw SadBunny's post, I knew immediately what to do (which, incidentally, was what you suggest).

Samuel

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