Jump to content

_IsPressed in a loop + abnormal conditions?


Recommended Posts

; ....

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)
Link to comment
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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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

; ...

 

 

Link to comment
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?

Link to comment
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. 

Link to comment
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.

Link to comment
Share on other sites

  • Developers
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 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

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 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.

Link to comment
Share on other sites

  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.

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

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