Jump to content

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


Adele
 Share

Recommended Posts

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
Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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