Jump to content

Recommended Posts

Posted

I am trying to make function which will check if shift key is pressed. I wrote the script but it does not work properly in some cases.

When I run this script from scite or from run option with SHIFT key pressed down, it suppose to show me gui message "You can now release SHIFT key" but I can't see it on the screen. It is hidden by other window opened before.

#include <Misc.au3>
#include <WindowsConstants.au3>

WaitForShiftKey()

Func WaitForShiftKey()
    If _IsPressed("10") Then
        Beep(500, 500)
        Local $hGUI_ShiftPressed = GUICreate(@ScriptName, 250, 10)
        GUISetStyle($WS_POPUPWINDOW, $WS_EX_TOPMOST, $hGUI_ShiftPressed)
        Local $idLabel = GUICtrlCreateLabel("You can now release SHIFT key...", 5, 5, 240)
        GUICtrlSetFont($idLabel, 9, 600, 0, "Segoe UI")
        GUISetState(@SW_SHOW, $hGUI_ShiftPressed)

        While _IsPressed("10") ;shift = 10, ctrl = 11, alt = 12
            Sleep(10)
        WEnd

        GUIDelete($hGUI_ShiftPressed)
        Return 1
    EndIf
EndFunc

The problem can be fixed when I add "msgbox(0,0,0)" code on the first line of function and start hold shift key when i see msgbox and then click "ok" button.

Maybe there is something wrong with my pc or I miss something?

Posted (edited)

Hi maniootek,

I could reproduce your described behavior. Please try the following version which works for me without an additional messages box.

#include <Misc.au3>
#include <WindowsConstants.au3>

WaitForShiftKey()

Func WaitForShiftKey()
    If _IsPressed("10") Then
        Beep(500, 500)

        Local $hGUI_ShiftPressed = GUICreate(@ScriptName, 250, 20, Default, Default, $WS_POPUPWINDOW, $WS_EX_TOPMOST)
        Local $idLabel = GUICtrlCreateLabel("You can now release SHIFT key...", 5, 5, 240)
        GUICtrlSetFont($idLabel, 9, 600, 0, "Segoe UI")
        GUISetState(@SW_SHOW, $hGUI_ShiftPressed)

        While _IsPressed("10") ;shift = 10, ctrl = 11, alt = 12
            Sleep(10)
        WEnd

        GUIDelete($hGUI_ShiftPressed)
        Return 1
    EndIf
EndFunc

Basically I increased the gui height from 10 to 20 and (that's the important part) create the gui with the styles directly instead of using the GUISetStyle() function.
This was only a tryout, but it seems to be working as expected in my case.

Please let me know if this works for you too, thanks :).

Best regards
Sven

________________
Stay innovative!

Edited by SOLVE-SMART

==> AutoIt related: ๐Ÿ”— GitHub, ๐Ÿ”— Discord Server, ๐Ÿ”— Cheat Sheet

  Reveal hidden contents
Posted
  On 1/21/2022 at 10:57 AM, SOLVE-SMART said:

Hi maniootek,

I could reproduce your described behavior. Please try the following version which works for me without an additional messages box.

#include <Misc.au3>
#include <WindowsConstants.au3>

WaitForShiftKey()

Func WaitForShiftKey()
    If _IsPressed("10") Then
        Beep(500, 500)

        Local $hGUI_ShiftPressed = GUICreate(@ScriptName, 250, 20, Default, Default, $WS_POPUPWINDOW, $WS_EX_TOPMOST)
        Local $idLabel = GUICtrlCreateLabel("You can now release SHIFT key...", 5, 5, 240)
        GUICtrlSetFont($idLabel, 9, 600, 0, "Segoe UI")
        GUISetState(@SW_SHOW, $hGUI_ShiftPressed)

        While _IsPressed("10") ;shift = 10, ctrl = 11, alt = 12
            Sleep(10)
        WEnd

        GUIDelete($hGUI_ShiftPressed)
        Return 1
    EndIf
EndFunc

Basically I increased the gui height from 10 to 20 and (that's the important part) create the gui with the styles directly instead of using the GUISetStyle() function.
This was only a tryout, but it seems to be working as expected in my case.

Please let me know if this works for you too, thanks :).

Best regards
Sven

________________
Stay innovative!

Expand  

Sven, thank you for taking time to test my script. Your version is working.  GUI is visible on the screen when shift is pressed. I am surprised that only gui style define was the problem.

Anyway, there is still one problem with the script. When SHIFT key is depressed and gui is deleted the other explorer window becomes active. Can you try on your side too?

Posted (edited)

Hi again,

yes I was also confused about the gui style behavior. Anyway, I am not sure what do you mean by:

  Quote

the other explorer window becomes active

Expand  
  • I use VSCode to execute all my AutoIt stuff, also your example script.
    • So when I press F5 to run the script and right afterwards the SHIFT key, I get the GUI until the key is released.
    • Then the focus do not return to the last focused window (which were VSCode) => it choose the browser (in my case) as new/or returned focused window.

So if you mean this unexpected behavior - yes it's also the case for me.

Best regards
Sven

________________
Stay innovative!

Edited by SOLVE-SMART

==> AutoIt related: ๐Ÿ”— GitHub, ๐Ÿ”— Discord Server, ๐Ÿ”— Cheat Sheet

  Reveal hidden contents
Posted

I was curious to understand why @maniootek way was not working properly.  At first glance, it should, but it's not.

#include <GUIConstants.au3>
#include <WinAPISysWin.au3>

Local $hWnd = GUICreate(@ScriptName, 250, 30)
GUISetStyle($WS_POPUPWINDOW, $WS_EX_TOPMOST, $hWnd)
Local $idLabel = GUICtrlCreateLabel("You can now release SHIFT key...", 5, 5, 240)
GUICtrlSetFont($idLabel, 9, 600, 0, "Segoe UI")

GUISetState(@SW_SHOW, $hWnd)

Sleep(2000)

Local $Style = _WinAPI_GetWindowLong($hWnd, $GWL_STYLE)
ConsoleWrite(Hex($Style, 8) & @CRLF)
Local $Style = _WinAPI_GetWindowLong($hWnd, $GWL_EXSTYLE)
ConsoleWrite($Style & @CRLF)

GUIDelete()

Local $hWnd = GUICreate(@ScriptName, 250, 30, Default, Default, $WS_POPUPWINDOW, $WS_EX_TOPMOST)
Local $idLabel = GUICtrlCreateLabel("You can now release SHIFT key...", 5, 5, 240)
GUICtrlSetFont($idLabel, 9, 600, 0, "Segoe UI")
GUISetState(@SW_SHOW, $hWnd)

Sleep(2000)

Local $Style = _WinAPI_GetWindowLong($hWnd, $GWL_STYLE)
ConsoleWrite(Hex($Style, 8) & @CRLF)
Local $Style = _WinAPI_GetWindowLong($hWnd, $GWL_EXSTYLE)
ConsoleWrite($Style & @CRLF)

GUIDelete()

If you run this code, you will see that some styles are not applied correctly and some are not applied at all.  My understanding is that when you create a GUI, it will automatically apply some default styles and extended styles.  With the GUISetStyle, you are trying to remove all styles to be replaced by others.  Windows does not seem to allow some of the changes.  I have seen that in the past, not all styles can be applied on the fly.

Posted

Hi Nine,

of course, I guess you are right ๐Ÿ‘ . As I remember correctly, I had a similar outcome in the past regarding such styles.

  Quote

My understanding is that when you create a GUI, it will automatically apply some default styles and extended styles. With the GUISetStyle, you are trying to remove all styles to be replaced by others.

Expand  

Besides the explanation I am still excited what @maniootek will answer about the post above.

Best regards
Sven

________________
Stay innovative!

==> AutoIt related: ๐Ÿ”— GitHub, ๐Ÿ”— Discord Server, ๐Ÿ”— Cheat Sheet

  Reveal hidden contents

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
ร—
ร—
  • Create New...