المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Detecting music keys from keyboard in winform app



C# Programming
01-06-2010, 11:41 AM
Good morning.

I am trying to detect the music button events from the keyboard, but have been unsuccessful so far. I have the following code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;

namespace KPlayer
{
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
WMPLib.IWMPPlaylist pl;
//WMPLib.IWMPPlaylistArray plItems;
WMPLib.IWMPMedia media;

// Required designer variable
//private System.ComponentModel.Container components = null;

ArrayList ar = new ArrayList();
int intCount = 0;
List numbers = new List();

float currentSize = 10;
string currentFont = "DS-Digital";

public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();
}

[STAThread]
// Declare Windows API calls used to access Windows hooks
[DllImport("user32.dll")]

public static extern int SetWindowsHookEx(int hookType,
HookProc callback,
int instance,
int threadID);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int hookHandle, int code,
int wparam, int lparam);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int hookHandle);
[DllImport("user32.dll")]
public static extern int GetKeyState(int vKey);

// Fields, constants, and structures used by the keyboard hook.
int hookHandle = 0;
HookProc cb = null;

public const int WH_KEYBOARD = 2;

public const int HC_ACTION = 0;
public const int HC_NOREMOVE = 3;

public const int VK_CONTROL = 0x11;
public const int VK_LWIN = 0x5B;
public const int VK_RWIN = 0x5C;
public const int VK_APPS = 0x5D;
public const int VK_LSHIFT = 0xA0;
public const int VK_RSHIFT = 0xA1;
public const int VK_LCONTROL = 0xA2;
public const int VK_RCONTROL = 0xA3;
public const int VK_LMENU = 0xA4;
public const int VK_RMENU = 0xA5;
public const int VK_BROWSER_BACK = 0xA6;
public const int VK_BROWSER_FORWARD = 0xA7;
public const int VK_BROWSER_******* = 0xA8;
public const int VK_BROWSER_STOP = 0xA9;
public const int VK_BROWSER_SEARCH = 0xAA;
public const int VK_VOLUME_MUTE = 0xAD;
public const int VK_VOLUME_DOWN = 0xAE;
public const int VK_VOLUME_UP = 0xAF;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
public const int VK_MEDIA_STOP = 0xB2;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int KF_UP = 0x8000;
public const long KB_TRANSITION_FLAG = 0x80000000;
public const int VK_W = 0x57;

// Keyboard hook delegate
public delegate int HookProc(int code, int wparam, int lparam);
public int Proc(int code, int wparam, int lparam)
{
if (code == HC_ACTION)
{
switch (wparam)
{
case VK_BROWSER_BACK:
// Handle Back keyboard button here.
textBox1.Text += "Browser Back key caught" +
Environment.NewLine;
break;
case VK_BROWSER_FORWARD:
// Handle Forward keyboard button here.
textBox1.Text += "Browser Forward key caught" +
Environment.NewLine;
break;
case VK_BROWSER_*******:
// Handle ******* keyboard button here.
textBox1.Text += "Browser ******* key caught" +
Environment.NewLine;
break;
case VK_BROWSER_STOP:
// Handle Stop keyboard button here.
textBox1.Text += "Browser Stop key caught" +
Environment.NewLine;
break;
case VK_BROWSER_SEARCH:
// Handle Search keyboard button here.
textBox1.Text += "Browser Search key caught" +
Environment.NewLine;
break;
case VK_VOLUME_MUTE:
// Handle Mute keyboard button here.
textBox1.Text += "Volume Mute key caught" +
Environment.NewLine;
break;
case VK_VOLUME_DOWN:
// Handle Volume - keyboard button here.
textBox1.Text += "Volume Down key caught" +
Environment.NewLine;
break;
case VK_VOLUME_UP:
// Handle Volume + keyboard button here.
textBox1.Text += "Volume Up key caught" +
Environment.NewLine;
break;
case VK_MEDIA_NEXT_TRACK:
// Handle Next Track keyboard button here.
textBox1.Text += "Media Next Track key caught" +
Environment.NewLine;
break;
case VK_MEDIA_PREV_TRACK:
// Handle Previous Track keyboard button here.
textBox1.Text += "Media Previous Track key caught" +
Environment.NewLine;
break;
case VK_MEDIA_STOP:
// Handle Stop keyboard button here.
textBox1.Text += "Media Stop key caught" +
Environment.NewLine;
break;
case VK_MEDIA_PLAY_PAUSE:
// Handle PLAY keyboard button here.
textBox1.Text += "Media Play/Pause key caught" +
Environment.NewLine;
break;
}
}
return (CallNextHookEx(hookHandle, code, wparam, lparam));
}

private void Form1_Load(object sender, EventArgs e)
{
wmp.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
wmp.MediaError +=
new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);

}

I don't get any errors, but it doesn't appear to work. Any ideas?

WHEELS