473,327 Members | 2,090 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,327 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 5622
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.