Old 05-18-2019, 08:42 AM   #1
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default JS_WindowMessage_Post() not working for mouse clicks

Hey all,

I've been trying to send a mouse click via a script.
I'm focusing the Reaper main window. But this somehow doesn't work.
The mouse click isn't executed (can't see any effect), but JS_WindowMessage_Post returns true.

Code:
hwnd = reaper.JS_Window_GetForeground()
x, y = reaper.GetMousePosition()

reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONDOWN", 1, 0, x, y)
reaper.JS_WindowMessage_Post (hwnd, "WM_LBUTTONUP", 0, 0, x, y)
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-18-2019, 10:18 AM   #2
juliansader
Human being with feelings
 
Join Date: Jul 2009
Posts: 2,659
Default

Two issues:

1) Posting a window message places a message in the message queue of the window and returns without waiting for the thread to process the message. A true return value simply means that the message was successfully posted -- not that the window will obey the message.

2) The x and y coordinates should be client coordinates, not screen coordinates, so you should use ScreenToClient before posting.
juliansader is offline   Reply With Quote
Old 05-18-2019, 07:20 PM   #3
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

It's still not working and I have no idea why...


This is my current code. I somehow have the feeling that I'm missing some important piece of the puzzle:

Code:
hwnd = reaper.JS_Window_GetForeground()

x1, y1 = reaper.GetMousePosition()
x, y = reaper.JS_Window_ClientToScreen(fore, x1, y1 )

reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONDOWN", 1, 0, x, y)
reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONUP", 0, 0, x, y)
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-19-2019, 04:27 AM   #4
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 8,907
Default

Quote:
Originally Posted by _Stevie_ View Post
It's still not working and I have no idea why...


This is my current code. I somehow have the feeling that I'm missing some important piece of the puzzle:

Code:
hwnd = reaper.JS_Window_GetForeground()

x1, y1 = reaper.GetMousePosition()
x, y = reaper.JS_Window_ClientToScreen(fore, x1, y1 )

reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONDOWN", 1, 0, x, y)
reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONUP", 0, 0, x, y)
Not sure if that's it (didn't test) but
Code:
x, y = reaper.JS_Window_ClientToScreen(fore, x1, y1 )
should be, no?
Code:
x, y = reaper.JS_Window_ClientToScreen(hwnd, x1, y1 )
nofish is offline   Reply With Quote
Old 05-19-2019, 04:30 AM   #5
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Yeah sorry, it's correct in my script. I changed it only for pasting it in the forums
So, that's not the issue

Does the script work for you, when you execute it in Reaper?
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-19-2019, 04:58 AM   #6
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 8,907
Default

Quote:
Originally Posted by _Stevie_ View Post
Does the script work for you, when you execute it in Reaper?
I tried it meanwhile (with fore replaced by hwnd) and no, doesn't do here anything either.
No idea what's wrong though, sorry.
nofish is offline   Reply With Quote
Old 05-19-2019, 06:03 AM   #7
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Thanks nofish, at least I know it's not specific to my system
Edgemeal has it working in some of his scripts. I think I have to check those out...
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-20-2019, 12:28 AM   #8
lb0
Human being with feelings
 
Join Date: Apr 2014
Posts: 3,070
Default

Quote:
Originally Posted by _Stevie_ View Post
Thanks nofish, at least I know it's not specific to my system
Edgemeal has it working in some of his scripts. I think I have to check those out...
Hey Stevie,

According to Julian's post above - you need ScreenToClient not ClientToScreen.
__________________
Projects - Reascripts - Lua:
LBX Stripper | LBX Chaos Engine | LBX Floating FX Positioner | LBX SRD Smart Knobs
Donate via Paypal
lb0 is online now   Reply With Quote
Old 05-20-2019, 04:52 AM   #9
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Ohh whoops! Looked up the wrong function in the API... :O
Thanks dude, fingers crossed this will work now. Checking in a few!
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-20-2019, 07:36 AM   #10
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

*sigh*, still not working:

Code:
hwnd = reaper.JS_Window_GetForeground()
title = reaper.JS_Window_GetTitle( hwnd )

x1, y1 = reaper.GetMousePosition()

x, y = gfx.screentoclient(x1, y1)

reaper.ShowConsoleMsg(x1 .." " .. y1 .. "\n")
reaper.ShowConsoleMsg(x .." " .. y .. "\n")

reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONDOWN", 1, 0, x, y)
reaper.JS_WindowMessage_Post(hwnd, "WM_LBUTTONUP", 0, 0, x, y)
The values of x, x1 and y, y1 are the same btw. With the difference, that screentoclient adds a .0 at the end.

My suspicion is: the click is not executed at all.
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-20-2019, 07:52 AM   #11
juliansader
Human being with feelings
 
Join Date: Jul 2009
Posts: 2,659
Default

reaper.JS_Window_ScreenToClient(hwnd, x1, y1)
juliansader is offline   Reply With Quote
Old 05-20-2019, 08:47 AM   #12
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Still no click, nowhere
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-20-2019, 09:26 AM   #13
juliansader
Human being with feelings
 
Join Date: Jul 2009
Posts: 2,659
Default

I quickly checked it on a few windows, including the MIDI editor and IDE, and everything seems to work fine.

Perhaps the problem lies in GetForeground. AFAIK the foreground window is usually the top-level framed window, while the mouse clicks should be sent to the target child window.

Could you let us know which windows you are working with?
juliansader is offline   Reply With Quote
Old 05-20-2019, 12:41 PM   #14
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Oh geez, you could be right. In that case, the Reaper main window had focus.
I will check and let you know. Thanks for digging into it!
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-20-2019, 12:51 PM   #15
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Unfortunately, that didn't work, either.

May I ask: when I execute the script, then a left click should happen e.g.
the edit cursor should move, when I hover with the mouse over the MIDI editor
or arrange window, is that correct?
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-20-2019, 02:59 PM   #16
juliansader
Human being with feelings
 
Join Date: Jul 2009
Posts: 2,659
Default

Quote:
when I hover with the mouse over the MIDI editor
or arrange window, is that correct?
If you are just hovering over a window, try:
Code:
x, y = reaper.GetMousePosition()
w = reaper.JS_Window_FromPoint(x, y)
x, y = reaper.JS_Window_ScreenToClient(w, x, y)
reaper.JS_WindowMessage_Post(w, "WM_LBUTTONDOWN", 1, 0, x, y)
reaper.JS_WindowMessage_Post(w, "WM_LBUTTONUP", 0, 0, x, y)
If you then use Ctrl+S to run the script in the IDE, while hovering over the arrange view or piano roll, this should send a Ctrl+mouseclick to that window.


EDIT: Corrected silly error in function name.

Last edited by juliansader; 05-22-2019 at 02:06 PM.
juliansader is offline   Reply With Quote
Old 05-20-2019, 04:41 PM   #17
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Hah! That worked, thanks Julian!

Instead of

Code:
x, y = reaper.JS_Window_ClientFromScreen(w, x, y)
it was:

Code:
x, y = reaper.JS_Window_ScreenToClient(w, x, y)

Just documenting this here, if someone else reads this
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-20-2019, 07:46 PM   #18
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 150
Default

Thanks, didn't know the JS plugin can simulate the clicking of mouse before.

If I change WM_LBUTTONDOWN to WM_RBUTTONDOWN, it will simulate a right click. What else parameters can replace it and simulate more action from mouse and keyboard?
dsyrock is offline   Reply With Quote
Old 05-21-2019, 04:07 AM   #19
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Not sure if keyboard is possible but here are some mouse commands:
https://docs.microsoft.com/en-us/win...ut-mouse-input
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-21-2019, 05:14 AM   #20
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 150
Default

Quote:
Originally Posted by _Stevie_ View Post
Not sure if keyboard is possible but here are some mouse commands:
https://docs.microsoft.com/en-us/win...ut-mouse-input
Thanks.
dsyrock is offline   Reply With Quote
Old 05-22-2019, 12:17 AM   #21
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 150
Default

I tried this to simulate a left drag

Code:
x, y = reaper.GetMousePosition()
w = reaper.JS_Window_FromPoint(x, y)
xc, yc = reaper.JS_Window_ScreenToClient(w, x, y)
reaper.JS_WindowMessage_Post(w, "WM_LBUTTONDOWN", 1, 0, xc, yc)

reaper.JS_Mouse_SetPosition(x-200, y)
x, y = reaper.GetMousePosition()
xc, yc = reaper.JS_Window_ScreenToClient(w, x, y)
reaper.JS_WindowMessage_Post(w, "WM_LBUTTONUP", 0, 0, xc, yc)
But it didn't make a left drag, but a alt+left drag?
dsyrock is offline   Reply With Quote
Old 05-22-2019, 08:24 AM   #22
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 1,251
Default

Quote:
Originally Posted by dsyrock View Post
I tried this to simulate a left drag
But it didn't make a left drag, but a alt+left drag?
This test worked for me, it moved an item in arrange view to the right 100 pixels.

Code:
x = 568
y = 240

w = reaper.JS_Window_FromPoint(x, y)
xc, yc = reaper.JS_Window_ScreenToClient(w, x, y)

reaper.JS_WindowMessage_Send(w, "WM_LBUTTONDOWN", 1, 0, xc, yc)

for n = xc, xc + 100 do
  reaper.JS_WindowMessage_Send(w, "WM_MOUSEMOVE", 1, 0, n, yc)
end

reaper.JS_WindowMessage_Send(w, "WM_LBUTTONUP", 0, 0, xc, yc)

edit, This works same to move also,
Code:
for n = 1, 100 do
  reaper.JS_WindowMessage_Send(w, "WM_MOUSEMOVE", 1, 0, n, yc)
end

Last edited by Edgemeal; 05-22-2019 at 02:01 PM.
Edgemeal is offline   Reply With Quote
Old 05-22-2019, 08:32 AM   #23
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,172
Default

FWIW I'd recommend using Send rather than Post for this purpose. PostMessage is really only needed when sending windows messages from threads other than the main (UI) thread, which is more or less never in this context. Maybe you'd use PostMessage for just tossing a single message to some non-REAPER window and not caring about what happens as a result, but for anything else, SendMessage.
schwa is offline   Reply With Quote
Old 05-22-2019, 08:53 AM   #24
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Posts: 2,589
Default

Thanks for that info, schwa!
__________________
My Reascripts forum thread | My Reascripts on GitHub | Stephan Römer - film composer
If you wish to donate for my scripts: please consider an organization like: animal shelter, doctors without borders, UNICEF, etc...
_Stevie_ is offline   Reply With Quote
Old 05-22-2019, 12:32 PM   #25
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 150
Default

Thanks Edgemeal and schwa. And I figure it out what make my script turn to alt+drag instead of drag. I assign the test script to "Alt+1",so the alt key mess up the script.

And is it possible to make this work in two different windows? For example, drag a file from Media Explorer to main window

This doesn't work

Code:
x, y = reaper.GetMousePosition()
w = reaper.JS_Window_FromPoint(x, y)
xc, yc = reaper.JS_Window_ScreenToClient(w, x, y)
reaper.JS_WindowMessage_Send(w, "WM_LBUTTONDOWN", 1, 0, xc, yc)

reaper.JS_Mouse_SetPosition(x-400, y)

x, y = reaper.GetMousePosition()
wnew = reaper.JS_Window_FromPoint(x, y)
xc, yc = reaper.JS_Window_ScreenToClient(wnew, x, y)
reaper.JS_WindowMessage_Send(wnew, "WM_LBUTTONUP", 0, 0, xc, yc)
And one more question, what does wParamLow and wParamHigh mean?

Last edited by dsyrock; 05-22-2019 at 08:23 PM.
dsyrock is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:37 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.