Amadeusz Wieczorek
Amadeusz Wieczorek

Reputation: 3207

win32 PostMessage WM_APPCOMMAND sends multiple messages instead of one

I'm writing a small accessibility app which simulates certain keyboard gestures, such as volume up\down.

The goal is to send a single command.

In practice, the volume goes all the way up to 100%, as if user pressed a button for couple seconds or as if the message was dispatched multiple times.

This behavior is the same with both PostMessage and SendMessage, in both C and C# (using PInvoke)

C:

PostMessage(0xffff, 0x0319, 0, 0xa0000)

C#:

PostMessage(new IntPtr(0xffff), WindowMessage.WM_APPCOMMAND, (void*)0, (void*)0xa0000);

The meaning of parameters: send to all windows, message, no source, volume up

Question: How do I issue a command which would result in Windows adjusting volume by the smallest increment?


Additionally, I attempted using WP_KEYUP and WP_KEYDOWN, without success

// dispatch to all apps, message, wparam: virtual key, lparam: repeat count = 1
User32.PostMessage(new IntPtr(0xffff), User32.WindowMessage.WM_KEYDOWN, new IntPtr(0xaf000), new IntPtr(1)); 
User32.PostMessage(new IntPtr(0xffff), User32.WindowMessage.WM_KEYUP, new IntPtr(0xaf000), new IntPtr(1));

Upvotes: 0

Views: 558

Answers (1)

Amadeusz Wieczorek
Amadeusz Wieczorek

Reputation: 3207

The reason why the command is sent multiple times is, as pointed by Hans in the comment, I broadcasted it to all windows using 0xffff as first parameter. Every window handled it by increasing volume by a notch.

The solution to sending multiple messages is to send the message to either

  1. The shell handle GetShellWindow()
  2. The foreground window handle GetForegroundWindow()

Both handles adjusted the volume by one notch. GetDesktopWindow() did not work, though.

Upvotes: 2

Related Questions