How to build wParam and lParam of WM_KEYDOWN/UP message?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by Chappier » 12 Jun 2022, 09:27

I have build this GUI that intercept WM_KEYDOWN and WM_KEYUP messages and print their parameters values:

AutoHotkey_2022-06-12_11-14-42.png
AutoHotkey_2022-06-12_11-14-42.png (12.36 KiB) Viewed 2030 times

How i could 'programatically' build wParam and lParam to use with PostMessage?

I mean something similar to the maro of building WM_LBUTTONDOWN lParam

Code: Select all

lParam := (Y << 16) | (X & 0xffff) 

I did some research but most i could find was in c++ which sadly I don't know anything
https://docs.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input#keystroke-message-flags
https://stackoverflow.com/a/43313970

Code: Select all

OnMessage(0x0100, "WM_KEYDOWN")
OnMessage(0x0101, "WM_KEYUP")

Gui, +HwndhGui
Gui, Color, 0
Gui, Add, Text, cWhite, Type key to get params:
Gui, Font, s18 w900
Gui, Add, Edit, xm w130 h50 vEdit hWndEdit +Center ReadOnly cRed
Gui, Font, s9 w400



Gui, Add, Text,   xm   y+10        cWhite    , PostMessage Test
Gui, Add, Text,   xm   y+10        cWhite    , Msg:
Gui, Add, Edit,   x+10 yp h18 w50  vsmsg     , 0x100
Gui, Add, Text,   x+10 yp          cWhite    , wParam
Gui, Add, Edit,   x+10 yp h18 w50  vswParam  ,
Gui, Add, Text,   x+10 yp          cWhite    , lParam
Gui, Add, Edit,   x+10 yp h18 w50  vslParam  ,
Gui, Add, Button, x+10 yp h18 cWhite gSend, Send



Gui, Add, Text, xm   y170 cWhite +Border +Center Section w150, WM_KEYDOWN
Gui, Add, Text, xm   y+10 cWhite              , Key pressed:
Gui, Add, Text, x100 yp   cGreen vKey w100    , 
Gui, Add, Text, xm   y+10 cWhite              , wParam:
Gui, Add, Text, x100 yp   cGreen vwParam w100 , 
Gui, Add, Text, xm   y+10 cWhite              , lParam:
Gui, Add, Text, x100 yp   cGreen vlParam w100 , 



Gui, Add, Text, x+10 ys cWhite  +Border +Center w150, WM_KEYUP
Gui, Add, Text, x210 y+10 cWhite              , Key pressed:
Gui, Add, Text, x300 yp   cGreen vKey2 w100   , 
Gui, Add, Text, x210 y+10 cWhite              , wParam:
Gui, Add, Text, x300 yp   cGreen vwParam2 w100, 
Gui, Add, Text, x210 y+10 cWhite              , lParam:
Gui, Add, Text, x300 yp   cGreen vlParam2 w100, 



Gui, Show, w400 h300, PostMsg
Return

Send() {
   Global hGui

   GuiControlGet, msg   ,, smsg
   GuiControlGet, wParam,, swParam
   GuiControlGet, lParam,, slParam

   PostMessage, %msg%, %wParam%, %lParam%,, ahk_id %hGui%
}

WM_KEYDOWN(wParam, lParam, Msg, Hwnd) {
   Global hGui
   GuiControlGet, Edit, hWnd, Edit

   If (hWnd = Edit) or (hWnd = hGui) {
      wParam2   := Format("{:#x}", wParam)
      Key        = vk%wParam2%
      Key       := GetKeyName(Key)

      GuiControl,, Edit, %Key%
      GuiControl,, Key, %Key%
      GuiControl,, wParam, %wParam%
      GuiControl,, lParam, %lParam%
   }
}

WM_KEYUP(wParam, lParam, Msg, Hwnd) {
   Global hGui
   GuiControlGet, Edit, hWnd, Edit

   If (hWnd = Edit) or (hWnd = hGui) {

      wParam2   := Format("{:#x}", wParam)
      Key        = vk%wParam2%
      Key       := GetKeyName(Key)
      FileAppend, Key: %Key%`n`n,*

      GuiControl,, Key2, %Key%
      GuiControl,, wParam2, %wParam%
      GuiControl,, lParam2, %lParam%
   }
}

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by mikeyww » 12 Jun 2022, 10:37

Ideas are below.

Code: Select all

Gui, +AlwaysOnTop
Gui, Color, 0
Gui, Font, s9 cWhite
Gui, Add, Text  ,                                       , Type key to get params:
Gui, Font, s18 w900
Gui, Add, Edit  , cRed w130 h40 vEdit ReadOnly Center
Gui, Font, s9  w400
Gui, Add, Text  ,                                       , PostMessage Test
Gui, Add, Text  ,                                       , Msg:
Gui, Add, Edit  , x+m  w50   h18   vsmsg    c0
Gui, Add, Text  , x+m                                   , wParam
Gui, Add, Edit  , x+m  w50   h18   vswParam c0
Gui, Add, Text  , x+m                                   , lParam
Gui, Add, Edit  , x+m  w50   h18   vslParam c0
Gui, Add, Button, x+m        h18                        , Send
Gui, Add, Text  , w150 xm    y+20  Border Center Section, WM_KEYDOWN
Gui, Add, Text  , w100 xm    ys+30                      , Key pressed:
Gui, Add, Text  , wp                                    , wParam:
Gui, Add, Text  , wp                                    , lParam:
Gui, Font, cGreen
Gui, Add, Text  , w100 xs+90 ys+30 vKey
Gui, Add, Text  , wp               vwParam
Gui, Add, Text  , wp               vlParam
Gui, Font, cWhite
Gui, Add, Text  , w150       ys    Border Center Section, WM_KEYUP
Gui, Add, Text  , w100                                  , Key pressed:
Gui, Add, Text  , wp                                    , wParam:
Gui, Add, Text  , wp                                    , lParam:
Gui, Font, cGreen
Gui, Add, Text  , w100 xs+90 ys+30 vKey2
Gui, Add, Text  , wp               vwParam2
Gui, Add, Text  , wp               vlParam2
Gui, Show, x1200, PostMsg
OnMessage(0x0100, "WM_KEYDOWN"), OnMessage(0x0101, "WM_KEYUP")
Return

ButtonSend:
Gui, Submit, NoHide
MsgBox, 64, Values, wParam = %swParam%`n`nlParam  = %slParam%`n`nMsg      = %smsg%
; msgbox PostMessage, %msg%, %wParam%, %lParam%,, ahk_id %hGui%
Return

WM_KEYDOWN(wParam, lParam, msg) {
 Gui, Submit, NoHide
 GuiControl,, Edit   , % key := GetKeyName(Format("vk{:#x}", wParam))
 GuiControl,, Key    , % key
 GuiControl,, swParam, % wParam
 GuiControl,, slParam, % lParam
 GuiControl,, smsg   , % msg
 GuiControl,, wParam , % wParam
 GuiControl,, lParam , % lParam
}

WM_KEYUP(wParam, lParam, msg) {
 Gui, Submit, NoHide
 GuiControl,, Key2   , % key := GetKeyName(Format("vk{:#x}", wParam))
 ; GuiControl,, swParam, % wParam
 ; GuiControl,, slParam, % lParam
 ; GuiControl,, smsg   , % msg
 GuiControl,, wParam2, % wParam
 GuiControl,, lParam2, % lParam
 FileAppend, Key: %key%`n`n,*
}
Last edited by mikeyww on 12 Jun 2022, 10:45, edited 1 time in total.

Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by Chappier » 12 Jun 2022, 10:44

You just copied what i posted and added a msgbox, my goal is a function to build the parameters and not rely on creating a GUI to receive the msg and return the value

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by mikeyww » 12 Jun 2022, 10:49

OK. Sorry I do not know how you are trying to use your parameters, and why your current approach will not work. Others here will know more.

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by teadrinker » 12 Jun 2022, 10:50

Use this example:

Code: Select all

$F1:: SendKeys("hello")

SendKeys(keys) {
   static WM_KEYDOWN := 0x100
        , WM_KEYUP   := 0x101
   Loop, parse, keys
   {
      key := A_LoopField
      vk := GetKeyVK(key)
      repeatCount := 1
      sc := GetKeySC(key)
      prevState := 0 ; up — 0, down — 1
      transitionState := 0
      wParam := vk
      lParam := repeatCount | sc << 16 | prevState << 30 | transitionState << 31

      ControlGetFocus, FocusedCtrl, A
      PostMessage, WM_KEYDOWN, wParam, lParam, %FocusedCtrl%, A

      prevState := 1
      transitionState := 1
      lParam := repeatCount | sc << 16 | prevState << 30 | transitionState << 31
      PostMessage, WM_KEYUP, wParam, lParam, %FocusedCtrl%, A
   }
}

Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by Chappier » 12 Jun 2022, 11:00

teadrinker wrote:
12 Jun 2022, 10:50
Use this example:
Thank you! exactly what i was looking for, question:

Code: Select all

PostMessage, WM_KEYDOWN, wParam, lParam, %FocusedCtrl%, A
how this line evaluate the value of the variables without the %%?
I mean:

Code: Select all

PostMessage, %WM_KEYDOWN%, %wParam%, %lParam%, %FocusedCtrl%, A
Also how to send keycodes like F1, F2, backspace, enter?

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by teadrinker » 12 Jun 2022, 11:26

Chappier wrote: how this line evaluate the value of the variables without the %%?
Some parameters support an expression syntax:
PostMessage wrote:Parameters
Msg
The message number to send, which can be an expression. See the message list to determine the number.

wParam
The first component of the message, which can be an expression. If blank or omitted, 0 will be sent.

lParam
The second component of the message, which can be an expression. If blank or omitted, 0 will be sent.
Also how to send keycodes like F1, F2, backspace, enter?
I changed the function a little:

Code: Select all

$F1::
   SendKeys("h|e|l|l|o")
   Sleep, 1000
   SendKeys("BS|BS|BS|BS|BS")
Return

SendKeys(keys) {
   static WM_KEYDOWN := 0x100
        , WM_KEYUP   := 0x101
   Loop, parse, keys, |
   {
      key := A_LoopField
      vk := GetKeyVK(key)
      repeatCount := 1
      sc := GetKeySC(key)
      prevState := 0 ; up — 0, down — 1
      transitionState := 0
      wParam := vk
      lParam := repeatCount | sc << 16 | prevState << 30 | transitionState << 31

      ControlGetFocus, FocusedCtrl, A
      PostMessage, WM_KEYDOWN, wParam, lParam, %FocusedCtrl%, A

      prevState := 1
      transitionState := 1
      lParam := repeatCount | sc << 16 | prevState << 30 | transitionState << 31
      PostMessage, WM_KEYUP, wParam, lParam, %FocusedCtrl%, A
   }
}

Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by Chappier » 15 Jun 2022, 16:12

teadrinker wrote:
12 Jun 2022, 11:26
I changed the function a little:
Theres difference from the wParam and lParam used by WM_KEYDOWN and WM_CHAR?

When i modify the function to send WM_CHAR the keys are sended as uppercase, and modifier like F1, F2, F3, BACKSPACE, etc, didn't work

Code: Select all

      ControlGetFocus, FocusedCtrl, A
      PostMessage, 0x102, wParam, lParam, %FocusedCtrl%, A
I modified it to use WM_CHAR, because with WM_KEYDOWN if you be physically holding any key like for example LCTRL, the window receiving the message 'merge' the LCTRL + the key being sent.

Does the parameters need to be created with different values?

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by teadrinker » 15 Jun 2022, 18:59

Both parameters are different, see descriptions on MSDN: WM_CHAR, WM_KEYDOWN.

Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by Chappier » 16 Jun 2022, 04:51

teadrinker wrote:
15 Jun 2022, 18:59
Both parameters are different, see descriptions on MSDN: WM_CHAR, WM_KEYDOWN.
Do you know how to also build these params for WM_CHAR?

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by teadrinker » 16 Jun 2022, 06:49

Code: Select all

$F1:: SendKeys("h|e|l|l|o")

SendKeys(keys) {
   static WM_CHAR := 0x102
   Loop, parse, keys, |
   {
      key := A_LoopField
      code := Asc(key)
      repeatCount := 1
      sc := GetKeySC(key)
      prevState := 0
      transitionState := 0
      wParam := code
      lParam := repeatCount | sc << 16 | GetKeyState("Alt", "P") << 29 | prevState << 30 | transitionState << 31
      ControlGetFocus, FocusedCtrl, A
      PostMessage, WM_CHAR, wParam, lParam, %FocusedCtrl%, A
   }
}

Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by Chappier » 16 Jun 2022, 07:08

Thank you!
When sending WM_CHAR on the postmessage, theres no need to send WM_KEYUP?

Code: Select all

SendKeys("BACKSPACE")
is sending B

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by teadrinker » 16 Jun 2022, 08:45

WM_CHAR does not send keys, but chars. Backspace is not a char.

N_athan
Posts: 40
Joined: 21 Aug 2021, 16:40

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by N_athan » 24 Jun 2022, 13:04

teadrinker wrote:
12 Jun 2022, 11:26
Also how to send keycodes like F1, F2, backspace, enter?
I changed the function a little:
Amazing the function, it helps when controlsend doesn't work.
Im using it to fill an email in a control and i noted that its not sending @, it does send 2 instead, any idea?

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to build wParam and lParam of WM_KEYDOWN/UP message?

Post by teadrinker » 24 Jun 2022, 13:28

@ is not a key, it's a char. Just use Send {Text}@.

Post Reply

Return to “Ask for Help (v1)”