-1

So I'm building an app that will eventually become a sort of docked On Screen Keyboard containing user-specified keys (part of my car-puter project).

So far so good, I can P/invoke keybd_event just fine for most keys, including Windows, print screen, etc.

The problem: certain keys such as VK_LAUNCH_MEDIA_SELECT and VK_LAUNCH_APP1 aren't working at all.

I'm using the same call as the other keys, so why is Windows being daft with just these "special" keys?

Further clarification: I'm not trying to capture keypresses, I'm sending them.

NativeMethods.keybd_event(this.Settings.vKey, 0, 0 | NativeMethods.KEYEVENTF_EXTENDEDKEY, (IntPtr)0);
NativeMethods.keybd_event(this.Settings.vKey, 0, NativeMethods.KEYEVENTF_KEYUP | NativeMethods.KEYEVENTF_EXTENDEDKEY, (IntPtr)0);

Where vKey is a char of a virtual keycode, such as VK_LAUNCH_APP1 (0xB6). I'm not sure if EXTENDEDKEY is really necessary since I don't think those specific keys are considered "extended" (i.e. Right-alt, Right-shift, etc)

VK_LAUNCH_APP1 (on Windows 10) will start File Explorer, no "Vendor provided program" needs to be present (other than Windows). Similarly, VK_LAUNCH_MEDIA_SELECT (0xB5) opens Windows Media Player (or whatever media player you have set in the Settings control panel).

Tsaukpaetra
  • 579
  • 5
  • 26
  • While I'm not a C# kind of guy I'd imagine that posting your existing code that handles keyboard events might be useful to those who do understand C#. :) – Admiral Noisey Bottom May 28 '16 at 07:20
  • Where did you hear that you should call the `keybd_event` function? And how did you start making use of it without seeing the big banner in [the documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304.aspx) that says this function is obsolete and you should always use `SendInput` instead? I don't mean to pick on you in particular, but you're far from the only one I see. In fact, practically everyone who asks questions about synthesizing keyboard events (and there are a confusingly high number of them) suggest using the same obsolete function. – Cody Gray - on strike May 28 '16 at 11:14
  • @CodyGray because for the most part it" just works", and I don't need to do anything more fancy than "push button, release button"? SendInput is all "let's make a queue and lock things and here's a specific struct for this and don't use that struct for that, and did you know you could also do mouse things? And hey look shiny new thing that complicates what is essentially covered in an older function!" – Tsaukpaetra May 28 '16 at 18:24

2 Answers2

1

You can certainly send these keys, it just isn't very likely that it does anything because nobody is listening for them. Look at your keyboard first, you'd need a dedicated button whose caption reads something like "Launch". Pretty uncommon, if available at all then it would at least have a "Play" and "Stop" button. Usually only available on a laptop, set aside from the regular keyboard keys and located above the function keys. Or overlaid on the function keys, you'd have to press the Fn key first.

The only kind of machine I've seen that had these keys was a Sony VAIO laptop. If available then you also have to first run the vendor provided applet that configures the key, determines exactly which program runs when you press the key.

Another vendor provided program, activated at login time, then recognizes these keys and implements their action. This requires writing a Windows hook with SetWindowsHookEx() for WH_SHELL, paying attention to the HSHELL_APPCOMMAND notification. Not the kind of thing you can do in C#.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I'm not sending "vendor specific" keys, these are built into Windows and handled just fine (actually, I found them by using a Microsoft keyboard, which works out of the box with no extra programs, including Microsoft Keyboard and Mouse center BS). – Tsaukpaetra May 28 '16 at 19:43
  • Except I'm not necessarily talking about programs being run as Admin, and I'm not sending the keys to anything running as admin (or in other words, a different run context). Were it so, I couldn't do the other keys (like mute, volume up, Windows) either, which I can. – Tsaukpaetra May 29 '16 at 00:30
  • Explorer or a media player take care of mute/volume, they are simple. Not launch or app1, somebody else needs to do that. Not having that somebody else is the normal case. – Hans Passant May 29 '16 at 02:37
  • That's what I'm saying. Windows (at least on Windows 10) takes care of App1 and a few other keys by default, which I'm trying to have pushed and Windows react. – Tsaukpaetra May 29 '16 at 02:41
  • Sigh. Use the sample code I linked to in a console mode app. Use `AppCommand.Send(AppCommands.LaunchApp1);` in your Main() method. Describe **exactly** what happens. – Hans Passant May 29 '16 at 02:48
  • Nothing, because it's ignoring such messages when sent by the app. When I send it via an actual keyboard, it works just fine. https://snag.gy/s7mMBf.jpg Input via keyboard into an app such as Notepad.exe, these keys opened the web browser, opened File Explorer, and opened Media Player. – Tsaukpaetra May 29 '16 at 03:17
  • BTW, yes, the other commands such as Volume Up/Dn sent as app messages did work, so the code is working fine, just Windows is intentionally ignoring it. – Tsaukpaetra May 29 '16 at 03:18
  • Additionally, per https://www.microsoft.com/mspress/books/sampchap/6232.aspx#120 (Which tricks actually work, shocker), I can specify that an arbitrary program be launched from App1 by detailing AppKey\17 in the registry (which does work, no third party program needed/installed/listening), which works from a real keyboard, but not from the app. – Tsaukpaetra May 29 '16 at 03:30
  • So in summary you have a custom keyboard with a custom driver that requires hacking registry keys by hand and clearly does not use the standard way these kind of keys are processed. It is not very clear how you plan to get ahead with this, updating your question with this info would be a minimum requirement but surely you'll have to call Microsoft. Probably best just to use the keyboard. – Hans Passant May 29 '16 at 04:00
  • Nope, way off the mark, I think you're trolling me now. This is a Microsoft keyboard requiring no drivers or setup of any kind, sending standard key presses, key presses which I'm trying to send in the standard Microsoft way. I'm not writing a driver, I'm not hacking anything, not sure why you're even suggesting such things. – Tsaukpaetra May 29 '16 at 04:08
0

Use the following code:

keybd_event(VK_LAUNCH_MEDIA_SELECT, 0, 0, 0);
keybd_event(VK_LAUNCH_MEDIA_SELECT, 0, KEYEVENTF_KEYUP, 0);
Ton Plooij
  • 2,583
  • 12
  • 15