Jump to content

_IsPressed and Send issues


Recommended Posts

Here is the essence of a code I have. So I don't have to remember the shift/ctrl/alt key symbols, it will send the symbol of the key to the input box when I press it.

while 1
    If WinGetState($AppTitle, "Set Hotkeys") = 15 Then
        If _IsPressed("10") Then;Shift
            _PressedShift()
        EndIf
        If _IsPressed("11") Then;Control
            _PressedControl()
        EndIf
        If _IsPressed("12") Then;Alt
            _PressedAlt()
        EndIf
    EndIf
wend


Func _PressedShift();Shift
    Send("+", 1)
EndFunc
Func _PressedControl();Control
    Send("^", 1)
EndFunc
Func _PressedAlt();Alt
    Send("!", 1)
EndFunc

The problem is when I press the key, it just continues to send and I have to exit the program. How do I fix this?

Link to comment
Share on other sites

The code demonstrates the problem just fine... My guess is that the while loop is repeating itself very quickly, but the Send() function isn't keeping up (it's probably delayed -- Opt('SendKeyDelay')), which makes it look like it's going infinitely.

Example?

#include <misc.au3>

$i = 0
while 1
    if _ispressed("10") then ; shift pressed
        consolewrite($i & @CRLF)
        $i += 1
    endif
wend

Run the script in SciTE. You'll notice how large the number gets after a split second of "holding" Shift.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

Thanks ultima, you sent me in the right direction and I fixed it, but there was no sendkeydelay in the script. Here's what I did.

Func _PressedShift();Shift
            If TimerDiff($iTimerKeyPress) > 250 Then
                $iTimerKeyPress = TimerInit()
                Send("+", 1)
            EndIf
        EndFunc
        Func _PressedControl();Control
            If TimerDiff($iTimerKeyPress) > 250 Then
                $iTimerKeyPress = TimerInit()
                Send("^", 1)
            EndIf
        EndFunc
        Func _PressedAlt();Alt
            If TimerDiff($iTimerKeyPress) > 250 Then
                $iTimerKeyPress = TimerInit()
                Send("!", 1)
            EndIf
        EndFunc
Link to comment
Share on other sites

You're probably better of inserting a Sleep() statement outside of the if statement in the While loop, like so:

#include <misc.au3>

$i = 0
while 1
    if _ispressed("10") then ; shift pressed
        consolewrite($i & @CRLF)
        $i += 1
    endif
    sleep(100)
wend

Why? Well, assuming you're still using a loop similar to the one you had in the first post, it kills a LOT of CPU time needlessly (you don't need to know that quickly whether a key is pressed... right?).

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

Actually that is no good for me. 1, my loop is pretty large and 2, when I put a loop in it makes a lot of my other functions that rely on Case button presses very sluggish...and I only had a sleep(50). I actually took out the sleep() in the loop in this app a while ago.

Link to comment
Share on other sites

No need to use a static time delay

while 1
    If WinGetState($AppTitle, "Set Hotkeys") = 15 Then
        If _IsPressed("10") Then;Shift
            While _IsPressed("10")
                Sleep(10)
            WEnd
            _PressedShift()
        EndIf
        If _IsPressed("11") Then;Control
            While _IsPressed("11")
                Sleep(10)
            WEnd
            _PressedControl()
        EndIf
        If _IsPressed("12") Then;Alt
            While _IsPressed("12")
                Sleep(10)
            WEnd
            _PressedAlt()
        EndIf
    EndIf
wend
Func _PressedShift();Shift
    Send("+", 1)
EndFunc
Func _PressedControl();Control
    Send("^", 1)
EndFunc
Func _PressedAlt();Alt
    Send("!", 1)
EndFunc
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...