misrepresentative

_IsPressed in a loop + abnormal conditions?

29 posts in this topic

#1 ·  Posted (edited)

; ....

While 1

    Switch TrayGetMsg()
        Case $id_Exit
            Exit
        Case $id_Test
            _test()
    EndSwitch

    If _IsPressed("23", $USER32_DLL) Then
        _example()
    EndIf

WEnd

 

For example, it may not work as expected under abnormal conditions such as high CPU utilization. If you press End key ("23"), it's not guaranteed that key press is detected. Could you kindly recommend me a way to improve it?

(note: without using Hotkeyset, if possible.)

 

Many thanks in advance for you time. :)

Edited by misrepresentative
added another case (for better ilustration)

Share this post


Link to post
Share on other sites



Welcome to AutoIt and the forum!

user32.dll is the default. To which value do you set variable $USER32_DLL?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thank you, water. Sorry if I'm misunderstanding but does it really matter?

 

Local $USER32_DLL = DllOpen("user32.dll")
If $USER32_DLL = -1 Then
    MsgBox($MB_ICONERROR + $MB_OK, "Error- DllOpen", "Unable to open user32.dll for use." & @CRLF & "Application will now exit.")
    Exit
EndIf

OnAutoItExitRegister("On_Exit")
Func On_Exit()
    DllClose($USER32_DLL)
EndFunc   ;==>On_Exit

; ...

 

 

Share this post


Link to post
Share on other sites

Sorry for kind of bumping it. Maybe the issue is triggered by TrayGetMsg() as it does not poll tray timely enough?

Share this post


Link to post
Share on other sites

That's a pretty vague issue to solve.  Since _IsPressed is evaluating the state of the keyboard, it is necessarily going to execute at the whim of CPU scheduling.  A hotkey might be better as it should trigger an, "as on demand as possible" event...depending on what you are really looking to do.   Why isn't hotkey an option for you?

Share this post


Link to post
Share on other sites

I am launching applications in protected environment(s) for various testing purposes. Hotkeys are dangerous from a security point of view & not allowed to be set.

Share this post


Link to post
Share on other sites

You're not allowed to write code that is executed on keypress, so to work around that you are writing code that is executed on keypress? :huh2:


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Hotkeyset is dangerous if and ONLY if you have a blanket hotkeyset that is active as long as the script is running. The trick is have in your loop on the top level this:

  • sleep (100
  • if winactive then call a function that turns on the hotkeyset
  • a sub loop after that in the function that will look for what you want AND if the window is still active or not.
  • If the window you have the hotkeyset is no longer active - turn off the hotkeyset

This type of setup is very common in many applications that has keys set to do specific functions. For example pressing the F3 key in Firefox opens the text search function. 

1 person likes this

Share this post


Link to post
Share on other sites
23 minutes ago, SadBunny said:

You're not allowed to write code that is executed on keypress, so to work around that you are writing code that is executed on keypress? :huh2:

Disclaimer: I'm not a programmer. I'm breaking stuff as a hobby. Volunteering if you want. Do note that I did not create the environment. (It's like a Sandbox if you're familiar with the concept.)

Hope it doesn't go really that off-topic. Sorry about it.

I might be wrong but here's an example : it's possible to block input by setting hotkeys. Now, is it possible to use _IsPressed to block input? Probably not. Another reason is that in such protected environments, you need compatibility as well. It affects compatibility of applications. (If you find a way to block it then another method should be found.)

To me it seems more risky to set hotkeys at first-sight.

 

Please correct me if I'm wrong.

Share this post


Link to post
Share on other sites
1 minute ago, misrepresentative said:

To me it seems more risky to set hotkeys at first-sight.

Don't know what that means. You can set/disable the HotKeySet() at any time in the script so you are always in control.

Jos 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Yes, Jos. You are right but unknown application run as well. There is a higher risk with unknown applications.

 

Thanks.

Edited by misrepresentative

Share this post


Link to post
Share on other sites

Unknown and possibly malicious applications run in the environment. Trusted applications do not.

Share this post


Link to post
Share on other sites

I mean between the use of _IsPressed() and HotKeySet()? 
Well aware of security risks and keylogging, but then is this discussion in the amateur league, but are not going down that road here. ;)

Jos 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I think there is a misunderstanding. Sorry. English is not my native language.

It's simply not possible to use HotKeySet in my case. It is considered more risky with mentioned environment. I just can't use it-- it's blocked. :)

Share this post


Link to post
Share on other sites
47 minutes ago, Bert said:

Hotkeyset is dangerous if and ONLY if you have a blanket hotkeyset that is active as long as the script is running. The trick is have in your loop on the top level this:

  • sleep (100
  • if winactive then call a function that turns on the hotkeyset
  • a sub loop after that in the function that will look for what you want AND if the window is still active or not.
  • If the window you have the hotkeyset is no longer active - turn off the hotkeyset

This type of setup is very common in many applications that has keys set to do specific functions. For example pressing the F3 key in Firefox opens the text search function. 

What if the user intends to use the key at the same time? eg not working with active window

Thanks.

Share this post


Link to post
Share on other sites
23 minutes ago, misrepresentative said:

It is considered more risky with mentioned environment.

So back to my original question: Why? What is the difference? 

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Because you can block keyboard input with it. That's one example.  eg make it do nothing on all keyboard keys

 

:)

Share this post


Link to post
Share on other sites

Ok, I am going to exit this conversation as it's not making much sense to me and there are way better ways to accomplish that but that isn't going to be discussed here as that can be used for malicious purposes.

Good luck with your scripting! :)

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

@misrepresentative If you're script has a GUI, then you can use accelerator keys, they are only active when the GUI is active. If you're just monitoring for key presses using _IsPressed or _WinAPI_GetAsyncKetState then you're at the whim of the timing available to the script to read from the keyboard, this will be CPU dependent and also depends on whatever else might be running at the time.

BTW, you can block any/all keyboard input very easily without ever using HotKeys, so the prohibition you have against them is ludicrous.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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