Adele

(Solved) GUIRegisterMsg() Starts To Not Working After Adding A Control

8 posts in this topic

#1 ·  Posted (edited)

Hi everyone. I want to detect when any key is pressed with GUIRegisterMsg(). The issue is that, before adding any control to GUI, it works but when I added, it starts to not working. I somehow don't understand the issue's source.

For example, by this way it works without any issue :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

GUIRegisterMsg($WM_KEYDOWN, "KeyPress")
$hGUI = GUICreate("")

GUISetState()

While True
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func KeyPress()
    MsgBox(0, "", "A key has been pressed.")
EndFunc

But when I added a label, it starts to not working.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

GUIRegisterMsg($WM_KEYDOWN, "KeyPress")
$hGUI = GUICreate("")

;A control to test the issue, GUIRegisterMsg starts not working after it was added
GUICtrlCreateLabel("Test", 10, 10, 300, 300)

GUISetState()

While True
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func KeyPress()
    MsgBox(0, "", "A key has been pressed.")
EndFunc

I've searched and I couldn't find something interesting with this. Thank you in advance.

Edited by Adele

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Adele,

A static control cannot accept keyboard input.  One possible workaraound...

#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("")

;A control to test the issue, GUIRegisterMsg starts not working after it was added
local $lbl010 = GUICtrlCreateLabel("Test", 10, 10, 200, 200, $ss_sunken)

guictrlsetstate($lbl010,$GUI_DISABLE)

GUISetState()

GUIRegisterMsg($WM_KEYDOWN, "KeyPress")

While True
    $iMsg = GUIGetMsg()
    Switch $iMsg

        Case $GUI_EVENT_CLOSE
            ExitLoop

    EndSwitch

WEnd



Func KeyPress($hWnd, $iMsg, $wParam, $lParam)
    ConsoleWrite($wParam & ' - ' & $lParam & @CRLF)
    ;MsgBox(0, "", "A key has been pressed.")
EndFunc

Note - Please heed J1's observation about message handlers.  The doc is very specific about their use.

kylomas

EDIT: Please read the forum rules regarding keyloggers. 

Edited by kylomas
Additional info
1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

There isn't a special reason for I use label. I'll prepare a ordinary interface with using GUI controls like input, label... As well as when I pressed to any key, the program will detect that.

Share this post


Link to post
Share on other sites

You may not have seen my edit.  Please read the forum rules regarding keyloggers.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I don't make a program similar keylogger. Only the program's window is active, it should detect key pressing. I prepare a screensaver. I don't need to get which key was pressed. I need to detect when any key was pressed.

Edited by Adele

Share this post


Link to post
Share on other sites

Adele,

Glad to hear it.  You may be interested in _WinAPI_GetIdleTime.  See the Help file.

kylomas

1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

@kylomas Thank you, I guess this will help me. 

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