Jump to content

_ISPressed and Sleep(1)


Tim H.
 Share

Recommended Posts

(1st post)

I'm trying to write a routine that functions like a screen saver but instead of popping up a screen saver, displays a msgbox(). Testing for mouse movement is easy enough, but testing for keyboard activity has proven to be much more of a challenge.

This snippet from my script seems to function fine except, even with sleep set as low as 1, it pegs CPU utilization. I have to bump sleep time way up before I see utilizaton drop, but then the loop misses keystrokes. I'm hoping to launch this to about 6000 workstations. Having the CPU at 99% would likely burn up a few. Does anyone have any ideas how I can make this work?

Thanks for looking.

.........
While 1
   For $y = 1 To $s_keys[0]
   If _IsPressed ($s_keys[$y], $dll) Then
      $LastKeyTime = TimerInit()
   Else
      If TimerDiff($LastKeyTime) > $TimeOut Then
         ExitLoop(2)
      EndIf
   EndIf
   Sleep($CPURelief)
   Next
WEnd
........
Edited by Tim H.
Link to comment
Share on other sites

Hm I think you could work with HotKeySet(...), the keys don't work then, if you want to write something else, but if i understood your problem, you only want to stop your screensaver, when a key is pressed? Then it wouldn't matter if the keyboard works for other things.

You just have to do HotKeySet for every Key then with the "stopscreensaver" function.

I don't know if there are other ways to realize that with Autoit.

With C++ you can surely get a keyboardaction as a message (correct me, if i'm wrong^^) and autoit is C++ based, but i don't know, if you can get that message with autoit.

"It's easier to disintegrate an atom than a prejudice." (A.Einstein)---------------------------------------------------------------------------My C++ - tools:Tidy tool-->indents your c++ sourceCleanscript --> cleans autoit-code before compiling (co-author: peethebee)My tools:GUIBuilder-->build your window and get the source; german versionMy Games:OnlineGameCenter-->Online Chess and Connect4 with a rtf-chatSnake-->including a level editor to build your own levelsTetris-->the well known game, big funOther things:Tower of Hanoi-->perfect riddler with graphic output

Link to comment
Share on other sites

Dumb question, but why would one need something like this?

Is it really dumb? I think it's a real problem to check, if there was a keyboard action (without isPressed and HotKeySet), so why don't you just post a solution, if you know one, instead of call that dumb? Maybe you don't need that, for such a thing as Tim wanted it, but it's surely useful.

"It's easier to disintegrate an atom than a prejudice." (A.Einstein)---------------------------------------------------------------------------My C++ - tools:Tidy tool-->indents your c++ sourceCleanscript --> cleans autoit-code before compiling (co-author: peethebee)My tools:GUIBuilder-->build your window and get the source; german versionMy Games:OnlineGameCenter-->Online Chess and Connect4 with a rtf-chatSnake-->including a level editor to build your own levelsTetris-->the well known game, big funOther things:Tower of Hanoi-->perfect riddler with graphic output

Link to comment
Share on other sites

ups :) right ^^

"It's easier to disintegrate an atom than a prejudice." (A.Einstein)---------------------------------------------------------------------------My C++ - tools:Tidy tool-->indents your c++ sourceCleanscript --> cleans autoit-code before compiling (co-author: peethebee)My tools:GUIBuilder-->build your window and get the source; german versionMy Games:OnlineGameCenter-->Online Chess and Connect4 with a rtf-chatSnake-->including a level editor to build your own levelsTetris-->the well known game, big funOther things:Tower of Hanoi-->perfect riddler with graphic output

Link to comment
Share on other sites

Dumb question, but why would one need something like this?

He said it was for a screen saver right?

#Include <Misc.au3> 

While 1
    for $x = 2 to 7
        for $y = 0 to 15
            $key = hex($x, 1)&hex($y, 1)
            Select
                Case _IsPressed($key) 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('01') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('02') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('08') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('09') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('0d') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('10') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('12') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('13') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('14') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('1b') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('21') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('22') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('23') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('24') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('25') 
                        Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('26') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('27') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('28') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('2c') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('2d') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
                Case _IsPressed('2e') 
                    Msgbox(0,"", "A Key Was Pressed.")
                    ;TurnOffScreenSaver()
            EndSelect
        next
     next
WEnd

Replace the msgboxes with whatever you need to turn off screen saver

EDIT: whoops I accidentally put 'thens' on my case statements :)

Edited by Paulie
Link to comment
Share on other sites

Or something like this:

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

$FakeWindow = GUICreate("")
$MainWindow = GUICreate("Screensaver", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST, $FakeWindow)

GUISetOnEvent(-11, "_EventHandler")

WinSetTrans($MainWindow, "", 1)

GUIRegisterMsg(0x0100, "KEYPRESS")
GUISetState()

While 1
    Sleep(1000)
WEnd

Func KEYPRESS()
    Exit
EndFunc

Func _EventHandler()
    Switch @GUI_CtrlId
        Case -11, -3
            Exit
    EndSwitch
EndFunc

Func OnAutoItExit()
    MsgBox(0, "Exit", "A key has been pressed or the mouse has been moved. Goodbye!")
EndFunc

?

Edited by marfdaman

Don't take my pic to serious...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You Looked, but you did not see!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Link to comment
Share on other sites

As Tim said, that would need too much CPU, keyloggers surely better (how could i forget it?)^^

"It's easier to disintegrate an atom than a prejudice." (A.Einstein)---------------------------------------------------------------------------My C++ - tools:Tidy tool-->indents your c++ sourceCleanscript --> cleans autoit-code before compiling (co-author: peethebee)My tools:GUIBuilder-->build your window and get the source; german versionMy Games:OnlineGameCenter-->Online Chess and Connect4 with a rtf-chatSnake-->including a level editor to build your own levelsTetris-->the well known game, big funOther things:Tower of Hanoi-->perfect riddler with graphic output

Link to comment
Share on other sites

I'm not hoping to interact with the screen saver at all. My routine reads a text file on my company network into an array and displays it in a message box. I'd like it to display once every 4 hours ( twice per shift) but only display if the machine has been inactive for 10 minutes or so. I don't care if the screen saver comes on in the meantime since the user will see the pop-up message as soon as they touch the workstation.

The idea is that I can have someone in Marketing edit the message on some kind of routine basis. Everyone will see the message pop up once in a while and I don't have to be involve in the content of the message.

Everything functions fine the way I have it except for the CPU problem.

Thanks for your help.

Link to comment
Share on other sites

Larry, that should do perfectly. I'm not sure how it's doing what it's doing, but the result are exactly what I'm looking for. Zero cpu utilization and accurate key or mouse movement sensing. I'll incorporate it into my script tomorrow and let you know how it works.

Thanks!

Link to comment
Share on other sites

#include <misc.au3>
While 1
    sleep(10)
    For $i = 0 to 255
        if (_IsPressed(Hex($i))) Then
            MsgBox(0, "", "You pressed the: " & Chr($i) & " Key!")
        EndIf
    Next
WEnd

here is something to check ever key... Simpler than a case statement for every character...

Edited by CHRIS95219
Link to comment
Share on other sites

  • 7 months later...

here is a knockoff of something that gafrost did. It tooltips the amount of idle seconds that have gone by...

HotKeySet("{Esc}", "_Terminate")

Dim $last_active = 0
Dim $timer = TimerInit()

While (1)
    $not_idle = _CheckIdle($last_active)
    If $not_idle <> 0 Then $timer = TimerInit()
    ToolTip(Int(TimerDiff($timer)/1000))
    Sleep(200)
WEnd

Func _CheckIdle(ByRef $last_active, $start = 0)
    $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    If $start Then
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        $last_active = DllStructGetData($struct, 2)
        Return $last_active
    Else
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        If $last_active <> DllStructGetData($struct, 2) Then
            Local $save = $last_active
            $last_active = DllStructGetData($struct, 2)
            Return $last_active - $save
        EndIf
    EndIf
EndFunc ;==>_CheckIdle

Func _Terminate()
    Exit
EndFunc ;==>_Terminate oÝ÷ Ûú®¢×£(ºWHºÇ­ç(uæ¢÷­¡ì^rë^¶¡×zZ0iû^¯Ý,yÊ'vÊÙ^¶)«"q©eÈý»§)àjH¨²F§u«çnçmjÉ"vWÅç.¶*'v¬mÂ䦺)z¹r#fbaÌ"V®¶­sdFð bvä7FfFRgV÷C´çFW&æWBgV÷C²ÂgV÷C²gV÷C²FVà &ö6W746Æ÷6RgV÷C´UÄõ$RäURgV÷C² b33c¶WG'Òb33c¶WG'Ò VÇ6P b33c¶WG'Ò VæD`¥VçFÂb33c¶WG'Ò ¥6ÆVW §'VâgV÷C´3¢b3#µ&öw&ÒfÆW2b3#´çFW&æWBWÆ÷&W"b3#´UÄõ$RäURÖæWrÖ²GG¢ò÷wwrævöövÆRæ6öÒgV÷C²ÂgV÷C²gV÷C²

Sorry, I'm still very new to this whole scripting bit.

-mjg

Link to comment
Share on other sites

Or perhaps a simple timer..

$timer = TimerInit()
While 1
    Sleep(5)
    ToolTip(TimerDiff($timer))
    If TimerDiff($timer) > 5000 Then
        Msgbox(0,"","5 seconds have gone by, let's restart")
        $timer = 0
        $timer = TimerInit()
    EndIf
WEnd

I don't like using sleep if you want the script to stall for 10+ seconds. Reason: you check nothing/do nothing during a sleep, whereas a timer allows for you to control everything, even within the sleep period.

Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

HotKeySet("{Esc}", "_Terminate")

Dim $last_active = 0
Dim $Inactive_Seconds = 90
Dim $timer = TimerInit()

$not_idle = _CheckIdle($last_active, 1)

While (1)
    $not_idle = _CheckIdle($last_active)
    If $not_idle <> 0 Then $timer = TimerInit()
    ToolTip(Int(TimerDiff($timer) / 1000))
    Sleep(200)
    If Int(TimerDiff($timer) / 1000) >= $Inactive_Seconds Then
        _KillIExplore()
        $timer = TimerInit()
        $not_idle = _CheckIdle($last_active, 1)
    EndIf
    
    
WEnd

Func _CheckIdle(ByRef $last_active, $start = 0)
    $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    If $start Then
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        $last_active = DllStructGetData($struct, 2)
        Return $last_active
    Else
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        If $last_active <> DllStructGetData($struct, 2) Then
            Local $save = $last_active
            $last_active = DllStructGetData($struct, 2)
            Return $last_active - $save
        EndIf
    EndIf
EndFunc   ;==>_CheckIdle

Func _KillIExplore($ietry = 10)
    Do
        If WinActivate("Internet", "") Then
            ProcessClose("IEXPLORE.EXE")
            $ietry = $ietry - 1
        Else
            $ietry = 0
        EndIf
    Until $ietry = 0

    Sleep(1000)

    Run(@ComSpec & ' /c start " " "http://www.google.com"', "", @SW_HIDE)

;~  Run("C:\Program Files\Internet Explorer\IEXPLORE.EXE -new -k http://www.google.com", "")
EndFunc   ;==>_KillIExplore

Func _Terminate()
    Exit
EndFunc   ;==>_Terminate

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

HotKeySet("{Esc}", "_Terminate")

Dim $last_active = 0
Dim $Inactive_Seconds = 90
Dim $timer = TimerInit()

$not_idle = _CheckIdle($last_active, 1)

While (1)
    $not_idle = _CheckIdle($last_active)
    If $not_idle <> 0 Then $timer = TimerInit()
    ToolTip(Int(TimerDiff($timer) / 1000))
    Sleep(200)
    If Int(TimerDiff($timer) / 1000) >= $Inactive_Seconds Then
        _KillIExplore()
        $timer = TimerInit()
        $not_idle = _CheckIdle($last_active, 1)
    EndIf
    
    
WEnd

Func _CheckIdle(ByRef $last_active, $start = 0)
    $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    If $start Then
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        $last_active = DllStructGetData($struct, 2)
        Return $last_active
    Else
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
        If $last_active <> DllStructGetData($struct, 2) Then
            Local $save = $last_active
            $last_active = DllStructGetData($struct, 2)
            Return $last_active - $save
        EndIf
    EndIf
EndFunc   ;==>_CheckIdle

Func _KillIExplore($ietry = 10)
    Do
        If WinActivate("Internet", "") Then
            ProcessClose("IEXPLORE.EXE")
            $ietry = $ietry - 1
        Else
            $ietry = 0
        EndIf
    Until $ietry = 0

    Sleep(1000)

    Run(@ComSpec & ' /c start " " "http://www.google.com"', "", @SW_HIDE)

;~  Run("C:\Program Files\Internet Explorer\IEXPLORE.EXE -new -k http://www.google.com", "")
EndFunc   ;==>_KillIExplore

Func _Terminate()
    Exit
EndFunc   ;==>_Terminate
Perfect! Thank you,
Link to comment
Share on other sites

  • 2 months later...

I too will be using this information, thank you very much gafrost you have clearly earned the title AutoIt MVP!!!

This should be put in the examples section to make it easier to find!

Edited by Skizmata

AutoIt changed my life.

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