Jump to content

problem with ispressed


MirnesC2
 Share

Recommended Posts

$timer = TimerInit()
While 1
$timed = TimerDiff($timer)
If _IsPressed("43", $dll) AND $timed >= 200 Then
    ConsoleWrite("c")
    $timer = TimerInit()
EndIf
WEnd

Okay so this little example code should give an out of "c" if pressed once and "cccc" if pressed 4 times. I added the little timer as my attempt to fix the problem I am having.

Without the timer, pressing "c" just once would output "ccccccccccccccc". I added a timer to make it not do that, that was my attempt at a fix. The only problem is that it cant be constant. If I type "cccc" really fast it will only output "cc". I type at different speeds so the out is always changing. Sometimes it outputs more then the amount of letters i actually hit. Example "cc" would do "ccc".

Is there any other solution to this besides the timer that will work better? Also another I have another problem. Is it possible to detect capital C? Like if I press both shift and c.

Edited by MirnesC2
Link to comment
Share on other sites

That's actually really easy, everything you need is _IsPressed :D

#include <Misc.au3>

$dll = DllOpen("user32.dll")

While 1
    Sleep(10);Because I don't like when scripts take 100% CPU
    If _IsPressed("43", $dll) Then
        While _IsPressed("43", $dll)
            Sleep(10);Because I don't like when scripts take 100% CPU
        WEnd
        ConsoleWrite("c" & @CRLF)
    EndIf
WEnd

And for the future, please post a working (as in runnable) reproducer. It's quite annoying having to write half the script to see whats wrong :huggles:

Link to comment
Share on other sites

That's actually really easy, everything you need is _IsPressed :mellow:

#include <Misc.au3>

$dll = DllOpen("user32.dll")

While 1
    Sleep(10);Because I don't like when scripts take 100% CPU
    If _IsPressed("43", $dll) Then
        While _IsPressed("43", $dll)
            Sleep(10);Because I don't like when scripts take 100% CPU
        WEnd
        ConsoleWrite("c" & @CRLF)
    EndIf
WEnd

And for the future, please post a working (as in runnable) reproducer. It's quite annoying having to write half the script to see whats wrong :(

Wow thank you so much. So simple, kind of mad I didn't think of that. And I didn't know the timer functions took up so much cpu. Thank you!!
Link to comment
Share on other sites

And I didn't know the timer functions took up so much cpu. Thank you!!

They don't. But your app only idles when you do a Sleep() so you would be looping hundreds if not thousands of times per second without them.

:mellow:

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