473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Media keys...

I want to be able to play/pause/next/back the tracks in whatever media
player app is running. So if iTunes is running, it will receive those
commands, if Windows Media Player is, it will receive those commands,
and so on, just like my multimedia keyboard does. I can't find any way
to do this. I have tried sending:

System.Windows.Forms.Keys.MediaPlayPause,
System.Windows.Forms.Keys.MediaPreviousTrack, ect.

but it didn't do anything. Is there a way to send a key that will
control any open media player app, or do I have to have my app check
all the open apps and use the appropriate method for each app to
control it? Any suggestions? Thanks.
Oct 7 '08 #1
3 5647
hu******************@yahoo.com wrote:
I want to be able to play/pause/next/back the tracks in whatever media
player app is running. So if iTunes is running, it will receive those
commands, if Windows Media Player is, it will receive those commands,
and so on, just like my multimedia keyboard does. I can't find any way
to do this. I have tried sending:

System.Windows.Forms.Keys.MediaPlayPause,
System.Windows.Forms.Keys.MediaPreviousTrack, ect.

but it didn't do anything. Is there a way to send a key that will
control any open media player app, or do I have to have my app check
all the open apps and use the appropriate method for each app to
control it? Any suggestions? Thanks.
SendKeys would work if the media keys were supported, but they're not.
Instead, you can call SendInput() yourself by P/Invoking. Here's a short but
complete sample as a console application. It uses .NET 3.5 and LINQ, but
this is just for convenience and you should have no trouble adapting it to
..NET 2.0 if required.

using System;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;

[StructLayout(LayoutKind.Sequential)]
public struct INPUT {
public int type;
public INPUTUNION inputUnion;
}

[StructLayout(LayoutKind.Explicit)]
public struct INPUTUNION {
// Fields
[FieldOffset(0)]
public HARDWAREINPUT hi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public MOUSEINPUT mi;
}

[StructLayout(LayoutKind.Sequential)]
public struct HARDWAREINPUT {
public int uMsg;
public short wParamL;
public short wParamH;
}

[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT {
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT {
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

static class NativeMethods {
[DllImport("user32.dll", SetLastError = true)]
public static extern int SendInput(int nInputs, [In] INPUT[] pInputs,
int cbSize);
}

class Program {
static int SendKeys(params Keys[] keys) {
var nativeKeys = keys.Select(
k =new INPUT {
type = 1, inputUnion = new INPUTUNION {
ki = new KEYBDINPUT { wVk = (short) k }
}
}
).ToArray();
int keysSent = NativeMethods.SendInput(nativeKeys.Length, nativeKeys,
Marshal.SizeOf(typeof(INPUT)));
if (keysSent == 0) throw new Win32Exception();
return keysSent;
}

static void Main(string[] args) {
while (true) {
var cki = Console.ReadKey();
switch (cki.KeyChar) {
case 'q': Environment.Exit(0); break;
case ' ': SendKeys(Keys.MediaPlayPause); break;
case 'n': SendKeys(Keys.MediaNextTrack); break;
case 'p': SendKeys(Keys.MediaPreviousTrack); break;
case 's': SendKeys(Keys.MediaStop); break;
}
}
}
}

--
J.
Oct 8 '08 #2
Thanks. I've tried using sendInput before, as well as keybd_event and
neither seems to do anything for the media keys. I'm trying it with
iTunes and Media Player, could it be that those 2 programs ignore
those keys, or should it work?
On Oct 8, 2:38*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
hurricane_number_...@yahoo.com wrote:
I want to be able to play/pause/next/back the tracks in whatever media
player app is running. So if iTunes is running, it will receive those
commands, if Windows Media Player is, it will receive those commands,
and so on, just like my multimedia keyboard does. I can't find any way
to do this. I have tried sending:
System.Windows.Forms.Keys.MediaPlayPause,
System.Windows.Forms.Keys.MediaPreviousTrack, ect.
but it didn't do anything. Is there a way to send a key that will
control any open media player app, or do I have to have my app check
all the open apps and use the appropriate method for each app to
control it? Any suggestions? Thanks.

SendKeys would work if the media keys were supported, but they're not.
Instead, you can call SendInput() yourself by P/Invoking. Here's a short but
complete sample as a console application. It uses .NET 3.5 and LINQ, but
this is just for convenience and you should have no trouble adapting it to
.NET 2.0 if required.

* *using System;
* *using System.Linq;
* *using System.Windows.Forms;
* *using System.Runtime.InteropServices;
* *using System.Collections.Generic;
* *using System.ComponentModel;

* *[StructLayout(LayoutKind.Sequential)]
* *public struct INPUT {
* * *public int type;
* * *public INPUTUNION inputUnion;
* *}

* *[StructLayout(LayoutKind.Explicit)]
* *public struct INPUTUNION {
* * *// Fields
* * *[FieldOffset(0)]
* * *public HARDWAREINPUT hi;
* * *[FieldOffset(0)]
* * *public KEYBDINPUT ki;
* * *[FieldOffset(0)]
* * *public MOUSEINPUT mi;
* *}

* *[StructLayout(LayoutKind.Sequential)]
* *public struct HARDWAREINPUT {
* * *public int uMsg;
* * *public short wParamL;
* * *public short wParamH;
* *}

* *[StructLayout(LayoutKind.Sequential)]
* *public struct KEYBDINPUT {
* * *public short wVk;
* * *public short wScan;
* * *public int dwFlags;
* * *public int time;
* * *public IntPtr dwExtraInfo;
* *}

* *[StructLayout(LayoutKind.Sequential)]
* *public struct MOUSEINPUT {
* * *public int dx;
* * *public int dy;
* * *public int mouseData;
* * *public int dwFlags;
* * *public int time;
* * *public IntPtr dwExtraInfo;
* *}

* *static class NativeMethods {
* * *[DllImport("user32.dll", SetLastError = true)]
* * *public static extern int SendInput(int nInputs, [In] INPUT[] pInputs,
int cbSize);
* *}

* *class Program {
* * *static int SendKeys(params Keys[] keys) {
* * * *var nativeKeys = keys.Select(
* * * * *k =new INPUT {
* * * * * *type = 1, inputUnion = new INPUTUNION {
* * * * * * *ki = new KEYBDINPUT { wVk = (short) k }
* * * * * *}
* * * * *}
* * * *).ToArray();
* * * *int keysSent = NativeMethods.SendInput(nativeKeys.Length, nativeKeys,
Marshal.SizeOf(typeof(INPUT)));
* * * *if (keysSent == 0) throw new Win32Exception();
* * * *return keysSent;
* * *}

* * *static void Main(string[] args) {
* * * *while (true) {
* * * * *var cki = Console.ReadKey();
* * * * *switch (cki.KeyChar) {
* * * * * *case 'q': Environment.Exit(0); break;
* * * * * *case ' ': SendKeys(Keys.MediaPlayPause); break;
* * * * * *case 'n': SendKeys(Keys.MediaNextTrack); break;
* * * * * *case 'p': SendKeys(Keys.MediaPreviousTrack); break;
* * * * * *case 's': SendKeys(Keys.MediaStop); break;
* * * * *}
* * * *}
* * *}
* *}

--
J.
Oct 9 '08 #3
hu******************@yahoo.com wrote:
Thanks. I've tried using sendInput before, as well as keybd_event and
neither seems to do anything for the media keys. I'm trying it with
iTunes and Media Player, could it be that those 2 programs ignore
those keys, or should it work?
I can't test iTunes but I tested it with Winamp first, which worked fine.
I've now extended the test to Winamp, Media Player and Media Player Classic.

Winamp responds whether it has focus or not. In fact, if Winamp is running,
only Winamp will respond to the media player keys. Other media players don't
get to see the keys even if they're in focus. I'm guessing Winamp gets it
done with a global keyboard hook.

Media Player doesn't respond to the test program. Media Player does respond
if you touch the key physically, whether it has focus or not.

Media Player Classic only responds to the keys if it has focus. This means
the program doesn't work on it either. Pressing a media key when it's not in
focus does nothing.

Bottom line? Every application seems to have its own idea of when to respond
to the media keys. I don't know what the "right" way of doing what you want
is, if there is such a way.

--
J.
Oct 9 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Garmt de Vries | last post by:
For a long time, I've used CSS to style my webpages, but only for media "screen" and "print". Now I've started looking into styling them for other media like "projection" and "handheld". I'd be...
11
by: Wolfgang Meier | last post by:
Hi, I really hope I'm not hitting a frequently asked question here, because I think almost every author must have made that decision. Anyway, here goes: Would it be better to write one global...
3
by: Devron Blatchford | last post by:
Hi there, Can someone tell me how I can detect media buttons (play, stop etc) in VB.NET. KeyPress, up, down events don't seem to capture them. Thanks Devron
2
by: Eukanuba863 | last post by:
hey guys first off i love what you are doing with the site!!! You guys are awesome Hey i want to put windows media play in a form with a web browser in it. I have the com component from...
4
by: subeen | last post by:
Hi, I am trying to parse a rss similar to the one found here (in example) http://www.feedforall.com/mediarss.htm <!-- Snipped for Brevity --> <item> <title>FeedForAll's Show Tunes and...
0
by: Konrad | last post by:
Hi everyone. I've got problem with converting xml file to xsd schema. The Xml file looks like: <?xml version="1.0" encoding="iso-8859-2"?> <rss version="2.0"...
0
by: kplazinski | last post by:
Hi everyone. I've got problem with converting xml file to xsd schema. The Xml file looks like: <?xml version="1.0" encoding="iso-8859-2"?> <rss version="2.0"...
0
by: kplazinski | last post by:
Hi everyone. I've got problem with converting xml file to xsd schema. The Xml file looks like: <?xml version="1.0" encoding="iso-8859-2"?> <rss version="2.0"...
0
by: sonia.sardana | last post by:
I have two buttons Play & Full Screen. If u click on Play Button,Movie starts..Movie Starts within 15-20 Seconds.....When u clik on Full Screen Button.,Media Player is Maximised. I want the if the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.