Jump to content
Sign in to follow this  
Champak

_IsPressed and Send issues

Recommended Posts

Champak

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?

Share this post


Link to post
Share on other sites
BillLuvsU

how about you give us the actual code? As far as I can see there is no error in that one.


[center][/center]Working on the next big thing.Currently Playing: Halo 4, League of LegendsXBL GT: iRememberYhslaw

Share this post


Link to post
Share on other sites
-Ultima-

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() ]

Share this post


Link to post
Share on other sites
Champak

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

Share this post


Link to post
Share on other sites
-Ultima-

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() ]

Share this post


Link to post
Share on other sites
Champak

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.

Share this post


Link to post
Share on other sites
DW1

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

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.