Jump to content
Sign in to follow this  
Lee Bussy

_IsPressed performance?

Recommended Posts

Lee Bussy

Here's a snippet of the code with which I am working:

While 1
    ; Check for keypress
    $dll = DllOpen("user32.dll")
    If _IsPressed("31", $dll) Then
            MsgBox(0,"Keypress", "'1' Key Pressed")
    ElseIf _IsPressed("32", $dll) Then
        MsgBox(0,"Keypress", "'2' Key Pressed")
    EndIf
    DllClose($dll)
    
    ; Check for button press / GUI messages
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnCast
            MsgBox(0,"Button","'1' Button Pushed")
        Case $btnLure
            MsgBox(0,"Button","'2' Button Pushed")
    EndSwitch
WEnd

In case it's not immediately obvious I have a form upon which I am expecting a button press, or a key press. I guess there's two questions:

  • Should I instantiate and destroy user32.dll each time through the loop as I have it here, or should I instantiate it before the loop and destroy it as a part of $GUI_EVENT_CLOSE? How would one profile one's code to determine the affect on performance of different strategies?
  • A more general question ... do I need to keep the 'Switch' and 'If' blocks separate (or should I) or is there an easier/cleaner way to do this?

Share this post


Link to post
Share on other sites
martin

Here's a snippet of the code with which I am working:

While 1
    ; Check for keypress
    $dll = DllOpen("user32.dll")
    If _IsPressed("31", $dll) Then
            MsgBox(0,"Keypress", "'1' Key Pressed")
    ElseIf _IsPressed("32", $dll) Then
        MsgBox(0,"Keypress", "'2' Key Pressed")
    EndIf
    DllClose($dll)
    
    ; Check for button press / GUI messages
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnCast
            MsgBox(0,"Button","'1' Button Pushed")
        Case $btnLure
            MsgBox(0,"Button","'2' Button Pushed")
    EndSwitch
WEnd

In case it's not immediately obvious I have a form upon which I am expecting a button press, or a key press. I guess there's two questions:

  • Should I instantiate and destroy user32.dll each time through the loop as I have it here, or should I instantiate it before the loop and destroy it as a part of $GUI_EVENT_CLOSE? How would one profile one's code to determine the affect on performance of different strategies?
  • A more general question ... do I need to keep the 'Switch' and 'If' blocks separate (or should I) or is there an easier/cleaner way to do this?

I would say that if you have DllOpen before and DllClose after every call to _IsPressed then you are not saving any time or processing power because that's what _IsPressed would have done anyway otherwise.

So I would suggest that you have DllOPen before the While loop, and have DllClose after the loop, or put it in an OnAutItExit function.

As far as the if loop inside the while is concerned, I don't see anything wrong with what you have done. To make the while loop easier to read you could have put the if loop bit in a function, say Check1_2(), but it's entirely a matter a choice. Also, since I'm such an untidy programmer, anything I suggest in this area can be taken very lightly. :)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Lee Bussy

So I would suggest that you have DllOPen before the While loop, and have DllClose after the loop, or put it in an OnAutItExit function.

That does seem cleanest ... the way I have it now is like fanning the door open and closed rapidly.

As far as the if loop inside the while is concerned, I don't see anything wrong with what you have done. To make the while loop easier to read you could have put the if loop bit in a function, say Check1_2(), but it's entirely a matter a choice. Also, since I'm such an untidy programmer, anything I suggest in this area can be taken very lightly. :)

My code starts out SO nice and neat ... and then goes downhill rapidly. It's a good thing nobody really has to review it after me. :-)

Thanks for taking the time to comment.

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.