New action proposition: send API message

x64 Replacement/Alternative to Microsoft's IntelliMouse application.
Post Reply
krzysiunet
New User
Posts: 8
Joined: Tue Mar 04, 2014 3:59 am

New action proposition: send API message

Post by krzysiunet » Tue Oct 10, 2017 1:29 am

A long time proposition, minor but powerful.

API messages are quite nice to achieve things. Usually API messages are used for advanced effects, yet for some tools it's the way how user could trigger the action.

The example would be MPC-HC. If you have it, check of keyboard shortcut option and look for the column ID. Sorry for promoting here other tools, but I think they nicely add up to themselves, I use them both. So here's the script in AutoHotKey (for interested people, it will be explained)

Code: Select all

#IfWinActive ahk_class MediaPlayerClassicW ; remove it to make keys global
MPCHC(msg) 
{
	SendMessage,0x0111,msg,,,ahk_class MediaPlayerClassicW
}
Backspace::MPCHC(907)
Enter::MPCHC(908)
When I press backspace it sends message 907, with enter it sends 908. By looking at the ID column of the shortcut options, it gives us volume up/down. Why not just simulated keystrokes? Because it's something that is not the same for every instance, it's the setting that user can change. With such script, I can change keyboard shortcuts in the MPC-HC, but the script will still work, because it directly triggers action, not keys. And such thing would be perfect. I know it's a lot to do. I'm just proposing, I don't need it currently that much, but that would make XMBC more powerful.

For such message, three fields would be needed:
* two integers* for message (in the example it sends 273 [0x0111 is hexadecimal] which means 'trigger user action' and then the proper code for the action
* window where message should be sent (XMBC has already great window selector, with classes, regexpes and wow, so it's basically done)

* as far as I remember, technically it was one integer, just high and low part, but it's been like 15 years since I had to go for such low level of developing, thankfully :D Oh well, but it was kinda fun and it made me understand how things works :)

User avatar
phil
Site Admin
Posts: 5156
Joined: Sun Apr 06, 2003 11:12 pm

Re: New action proposition: send API message

Post by phil » Tue Oct 10, 2017 7:58 am

Basically you want the ability to invoke SendMessage and or PostMessage directly to the active window - yes this would be possible (its how XMBC does a lot of the action internally). The problem is that it is quite "techy" and has the potential to confuse a lot of people - but its defiantly do-able and I could add two new actions to accomplish this - it gets more complicated if you want to chain messages (send more than one) such as a such as a key down and key up message... I've added this to my todo list and I'll have a think and see if I can do this with some sort of nice interface rather than an AHK style script (adding a scripting language would be overkill and too much work for me!). No promises or timescales yet!
--[ Phil ]--
--[ Administrator & XMBC Author ]--
Logitech G9, Logitech MX518, Microsoft Intellimouse, Trust 16341 BT Mouse
Windows 10 x64, Intel i5-6600k, Asus 170-A, 16GB DDR4,
nVidia GeForce GTX 970, 2xEvo 850 (RAID 0), 2x2TB WD Black (RAID1)

krzysiunet
New User
Posts: 8
Joined: Tue Mar 04, 2014 3:59 am

Re: New action proposition: send API message

Post by krzysiunet » Wed Oct 11, 2017 10:35 pm

Yeah, that's what I thought :) I'm not any authority, but as a simple user: making AHK like scripting language would kill XMBC*. So I totally agree with you, but not only because it would slow down developing other tools, but XMBC is good because it's not a scripting language.

For me AHK coexists with XMBC ideally. One is simple and every change takes seconds. Other one is illogical, doing simple things takes time, yet it's surely more powerful. I'm coding since 23 years, it doesn't matter if it's PHP or assembler, I script everything I can. But still I use XMBC as for what it does it's superior :)

I just thought of simulated keystrokes, like {POSTMSG:123}. "I've added this to my todo list (...) No promises or timescales yet" - that's even more than I've counted for! :D Thanks a lot! Possibility, even if unsure is still great :)

* Totally offtopic! if you ever checked out the syntax, error handling etc. of AHK, it's horrible. Instead triggering errors it runs - as a programmer you know what it means - random results virtually impossible to debug. Variable "foo2" is fine, but when "foo" is an array, then third element can be accessed by either foo[2] or... again foo2 :| It supports advanced C-alike DLL calls very well, but you need to use tricks if you want loop inside loop :D Conditional expressions can be done in about 5 ways, including a function and every way with same expression will give different results! When people learn basic things in AHK, they usually wants something more, they learn more advanced things, so their time in learning basics won't be lost. I'm not proud of knowing it.

User avatar
phil
Site Admin
Posts: 5156
Joined: Sun Apr 06, 2003 11:12 pm

Re: New action proposition: send API message

Post by phil » Wed Oct 11, 2017 10:39 pm

krzysiunet wrote:
Wed Oct 11, 2017 10:35 pm
I just thought of simulated keystrokes, like {POSTMSG:123}. "I've added this to my todo list (...) No promises or timescales yet" - that's even more than I've counted for! :D Thanks a lot! Possibility, even if unsure is still great :)
You know what, I didn't even think of that option... maybe that would be quicker to get in, in advance of a gui option...
--[ Phil ]--
--[ Administrator & XMBC Author ]--
Logitech G9, Logitech MX518, Microsoft Intellimouse, Trust 16341 BT Mouse
Windows 10 x64, Intel i5-6600k, Asus 170-A, 16GB DDR4,
nVidia GeForce GTX 970, 2xEvo 850 (RAID 0), 2x2TB WD Black (RAID1)

User avatar
phil
Site Admin
Posts: 5156
Joined: Sun Apr 06, 2003 11:12 pm

Re: New action proposition: send API message

Post by phil » Sat Oct 14, 2017 2:32 pm

I have added two new Simulated Keystrokes tags to 2.17 Beta 9
POSTWM and SENDWM

Let me know if it works (it seems to here).

Thanks,
Phil
--[ Phil ]--
--[ Administrator & XMBC Author ]--
Logitech G9, Logitech MX518, Microsoft Intellimouse, Trust 16341 BT Mouse
Windows 10 x64, Intel i5-6600k, Asus 170-A, 16GB DDR4,
nVidia GeForce GTX 970, 2xEvo 850 (RAID 0), 2x2TB WD Black (RAID1)

User avatar
injtsvetkov
Fanatic
Posts: 217
Joined: Mon Jun 06, 2016 8:51 am

Re: New action proposition: send API message

Post by injtsvetkov » Tue Oct 17, 2017 10:52 pm

krzysiunet wrote:
Tue Oct 10, 2017 1:29 am
The example would be MPC-HC. If you have it, check of keyboard shortcut option and look for the column ID. Sorry for promoting here other tools, but I think they nicely add up to themselves, I use them both. So here's the script in AutoHotKey (for interested people, it will be explained)

Code: Select all

#IfWinActive ahk_class MediaPlayerClassicW ; remove it to make keys global
MPCHC(msg) 
{
	SendMessage,0x0111,msg,,,ahk_class MediaPlayerClassicW
}
Backspace::MPCHC(907)
Enter::MPCHC(908)
When I press backspace it sends message 907, with enter it sends 908. By looking at the ID column of the shortcut options, it gives us volume up/down. Why not just simulated keystrokes? Because it's something that is not the same for every instance, it's the setting that user can change. With such script, I can change keyboard shortcuts in the MPC-HC, but the script will still work, because it directly triggers action, not keys. And such thing would be perfect. I know it's a lot to do. I'm just proposing, I don't need it currently that much, but that would make XMBC more powerful.
Hi mate, that's really interesting suggestion and it's great that Phil already added it to SimKeys! Can you please explain a little more (in your own words) about these shortcut IDs and how do they work. I googled it and I found some info and a list of windows API messages but I still have some basic questions. I was looking for shortcut IDs for excel but I couldn't find any, is there a tool which somehow "sniffs" the message ID of an action that I do? For example, when I use the charms bar in Win8 I go to Settings>Brightness and a brightness slider pops-up. When I scroll Up/Down over it the brightness changes by 2%. I suppose this scrolling over the slider sends such messages to windows and I wonder whether it is possible to somehow identify them and if it is, would it be possible to increase/decrease the brightness by the new POSTWM/SENDWM tags in SimKeys without opening the slider from the charms bar?

Thanks!
Iliya
HAMA Mirano
Windows 8.1 x64, Intel i5-3230M @ 2.60GHz, 4GB

User avatar
phil
Site Admin
Posts: 5156
Joined: Sun Apr 06, 2003 11:12 pm

Re: New action proposition: send API message

Post by phil » Tue Oct 17, 2017 11:00 pm

In anticipation krzysiunet's reply, here are my own thoughts....

Windows pretty much runs on messages - so pretty much everything is controlled by messages behind the scenes. However, there are so many that its mind blowing and not simple :). You really need some advanced knowledge of how Windows itself works and how applications use the various messages (and they don't all behave in the same way and application can define and register their own messages, for example, XMBC sends a custom registered message to its setup window to highlight the active profile in the profiles list, and another to highlight the button dropdowns in orange etc)

Having said that, I'm not sure about hardware controls such as brightness - that may be more hardware orientated (like driver IOCTRL which is different) - not sure. Then there is the destination of the messages - the new options in XMBC send the messages to the window under the cursor <only> right now. Sending to other places is difficult because you need the destination window handle which will be different every time the window is created, but maybe I need a broadcast option (send to all windows) we shall see as time goes on I guess.

There are several options for "spying" on messages. The one I use the most (and I have to do a lot of this for XMBC development :)) is Microsoft's own Spy++ (comes with visual studio - may be available separately).

Another good one (that I discovered recently) is APIMonitor (http://www.rohitab.com/apimonitor).

Thanks,
Phil
--[ Phil ]--
--[ Administrator & XMBC Author ]--
Logitech G9, Logitech MX518, Microsoft Intellimouse, Trust 16341 BT Mouse
Windows 10 x64, Intel i5-6600k, Asus 170-A, 16GB DDR4,
nVidia GeForce GTX 970, 2xEvo 850 (RAID 0), 2x2TB WD Black (RAID1)

User avatar
injtsvetkov
Fanatic
Posts: 217
Joined: Mon Jun 06, 2016 8:51 am

Re: New action proposition: send API message

Post by injtsvetkov » Wed Oct 18, 2017 12:59 am

Thank you Phil, I'm so "in the dark" here that it's even difficult to me to ask the right questions :lol:
phil wrote:
Tue Oct 17, 2017 11:00 pm
Having said that, I'm not sure about hardware controls such as brightness - that may be more hardware orientated (like driver IOCTRL which is different) - not sure. Then there is the destination of the messages - the new options in XMBC send the messages to the window under the cursor <only> right now. Sending to other places is difficult because you need the destination window handle which will be different every time the window is created, but maybe I need a broadcast option (send to all windows) we shall see as time goes on I guess.
Indeed the "destination of the messages" is one of the things that I can't understand right now. I mean what choices are there e.g. send to the active window (even if the cursor is away from it), send to the window under the cursor (no matter active or not), send to minimized window, send to Windows (globally), send to process ... or only a "window" can be a "destination" :roll:

Thankfully I'm at work right now and I realized that on the PC (Win8) the brightness icon is inactive and it says "Unavailable" instead of "Brightness" (like it does on my laptop) which is quite interesting but somehow it makes sense to me. I thought that it would be the same on both but is seems that this control is only for a "built-in" display (if I'm not mistaken). Still I think that there is a difference between the brightness control in XMBC (which changes the brightness by 10% like the "Fn" keys) and the one in the charms bar in a way that the first one is strictly hardware oriented and the second one somehow passes through Windows via some kind of messages. I might be wrong but I have to try and see, I hope I'll find some spare time soon to try those tools you mentioned and see if I can find some clues.

Thanks!
Iliya
HAMA Mirano
Windows 8.1 x64, Intel i5-3230M @ 2.60GHz, 4GB

User avatar
phil
Site Admin
Posts: 5156
Joined: Sun Apr 06, 2003 11:12 pm

Re: New action proposition: send API message

Post by phil » Wed Oct 18, 2017 7:51 am

injtsvetkov wrote:
Wed Oct 18, 2017 12:59 am
Indeed the "destination of the messages" is one of the things that I can't understand right now. I mean what choices are there e.g. send to the active window (even if the cursor is away from it), send to the window under the cursor (no matter active or not), send to minimized window, send to Windows (globally), send to process ... or only a "window" can be a "destination" :roll:
The clue is in the name, a Windows Message can be sent to any window.
XMBC only allows to send to the window under the cursor currently.
When you click a minimized window on the task bar, the task bar essentially sends a WM_SHOWWINDOW message to it telling it to restore. As th mouse moves across an active window, it receives a WM_MOUSEMOVE message for every new point (x/y pixel). When you click, you get a WM_LBUTTONDOWN or WM_LBUTTONUP (for the left button) these are what XMBC intercepts and remaps. When you change a system setting (like for example the double click time, Windows broadcasts a message to all windows saying that the system settings hav changed, allowing them to reload the settings and react to the new setting. There are 1000's of messages for everything, and then there are the undocumented app specific ones which sometimes you can figure out with Spy++ etc.
[/quote]
injtsvetkov wrote:
Wed Oct 18, 2017 12:59 am
Thankfully I'm at work right now and I realized that on the PC (Win8) the brightness icon is inactive and it says "Unavailable" instead of "Brightness" (like it does on my laptop) which is quite interesting but somehow it makes sense to me. I thought that it would be the same on both but is seems that this control is only for a "built-in" display (if I'm not mistaken). Still I think that there is a difference between the brightness control in XMBC (which changes the brightness by 10% like the "Fn" keys) and the one in the charms bar in a way that the first one is strictly hardware oriented and the second one somehow passes through Windows via some kind of messages. I might be wrong but I have to try and see, I hope I'll find some spare time soon to try those tools you mentioned and see if I can find some clues.
Th brightness options in windows require a compatible (probable LCD) monitor (and driver) that supports the brightness API in Windows. Its not strictly messages - its an API provided by MS that interacts directly with the monitor/display driver. XMBC uses the same brightness API ~(I think) as the charms bar - its not done via messages (typically you picked one of the few things that's not messages :)). That's not to say a window could not use messages to then interact with the brightness API, but that's not how the charms bar or XMBC do it (actually XMBC may use a message internally t to communicate between the hook thread and the main *GUI) thread to adjust the brightness. Of course sliding the charms bar slider will involve sending many messages, not least a scroll message from the slider to the host window, which if you could replicate (send to the charms bar window) in theory may allow you to adjust the slider. So I can see the next request, sending a message to a names window.

Windows have names (effectively titles) and classes... There is a FindWindow API can searches all Windows to find those with a specified name or class. But if your talking about that, then it gets quite complex to specify the search parameters and narrow down the results (you may get more than one etc.) which is perhaps when you want to be thinking wider than XMBC - use an autohotkey script or write a little program to do what you want. Its not somewhere I particularly want to go with XMBC due to the complications and endless support!)

Regards,
Phil
--[ Phil ]--
--[ Administrator & XMBC Author ]--
Logitech G9, Logitech MX518, Microsoft Intellimouse, Trust 16341 BT Mouse
Windows 10 x64, Intel i5-6600k, Asus 170-A, 16GB DDR4,
nVidia GeForce GTX 970, 2xEvo 850 (RAID 0), 2x2TB WD Black (RAID1)

User avatar
injtsvetkov
Fanatic
Posts: 217
Joined: Mon Jun 06, 2016 8:51 am

Re: New action proposition: send API message

Post by injtsvetkov » Sat Oct 21, 2017 8:01 am

Thank you for the info Phil!
It certainly cleared some things out so now I see exactly how far away I am from this matter :haha:
Windows have names (effectively titles) and classes... There is a FindWindow API can searches all Windows to find those with a specified name or class. But if your talking about that, then it gets quite complex to specify the search parameters and narrow down the results (you may get more than one etc.) which is perhaps when you want to be thinking wider than XMBC - use an autohotkey script or write a little program to do what you want. Its not somewhere I particularly want to go with XMBC due to the complications and endless support!)
No, no, I'm definitely not talking about those complicated things, especially as it turns out this is usable only for laptops (integrated monitors) there is absolutely no point. I'm just trying to understand the difference between the two brightness controls and whether there is a way to make one behave like the other. Now maybe there is and maybe there isn't but either way there has to be some logic behind.
So if it's not done by messages then how it is happening, I guess something like this: at Windows start-up it checks whether there is an integrated monitor and if it detects one it loads the brightness API which listens for some user action (pressing hardware button or using the charms menu) and if it detects one - it sends command to the driver to change the brightness :roll:
Both brightness controls should be interacting with the same API (there is no logic in having 2 different APIs for the same thing) but if the end result is different then there has to be some difference in the way each one is interacting with the API. Either each one is "telling something different" to the API or the API makes difference whether it receives input from HID (the kbd keys), SendInput API (if that's the way XMBC does it) or from the charms bar slider and thus sends different command to the driver.
Now if I move the slider with the scroll wheel it makes 2% change, but if I use the Up/Down arrows it makes 1% change, that's the same with both the charms bar slider and the one in the Power Options window, so I presume that in each case the slider window tells something different to the API.
Unfortunately sometimes my laptop does something strange, when I use the kbd keys or XMBC it shows the brightness indicator at the upper left corner of the screen but the brightness does not change. The indicator moves only one step up or down depending on whether I try to increase or decrease the brightness but it does not change at all no matter how many times I press the button. In that case I have to use the charms bar slider and the strange here is that it sometimes works and I'm able to adjust the brightness but sometimes it doesn't work too (I'm able to move the slider with the mouse by scrolling or dragging but the brightness doesn't change) so I have to reboot.
Here is something I found https://msdn.microsoft.com/en-us/librar ... s.85).aspx , maybe you have read it but maybe you haven't :)

Thanks,
Iliya
HAMA Mirano
Windows 8.1 x64, Intel i5-3230M @ 2.60GHz, 4GB

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests