1

随意编辑标题。

好的,所以我正在尝试创建一个短键按钮,可以跳到任何媒体播放器的下一首歌曲?就像某些键盘在 FN + F11 中所具有的一样,它会转到下一首歌曲,无论如何要将其集成到我自己的键盘的 c# 中?到目前为止我有这个代码,但它所做的只是将一个味精发布到 textbox1 上。

// Structure contain information about low-level keyboard input event 
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
    public Keys key;
    public int scanCode;
    public int flags;
    public int time;
    public IntPtr extra;
}
//System level functions to be used for hook and unhook keyboard input  
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern short GetAsyncKeyState(Keys key);
//Declaring Global objects     
private IntPtr ptrHook;
private LowLevelKeyboardProc objKeyboardProcess;

private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
{
    if (nCode >= 0)
    {
        KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));

        // Keys
        if (objKeyInfo.key == Keys.F10 && ModifierKeys == Keys.Alt)
        {
            textBox1.Focus();
            a += 1;
            c += 1;
            textBox1.Text += Convert.ToString(c) + ". " + "Previous Song" + Environment.NewLine;
            PrevSongCount.Text = Convert.ToString(a);
            AllUpCount.Text = Convert.ToString(c);
            return (IntPtr)1;
        }
        if (objKeyInfo.key == Keys.F11 && ModifierKeys == Keys.Alt)
        {
            textBox1.Focus();
            b += 1;
            c += 1;
            textBox1.Text += Convert.ToString(c) + ". " + "Next Song" + Environment.NewLine;
            NextSongCount.Text = Convert.ToString(b);
            AllUpCount.Text = Convert.ToString(c);
            return (IntPtr)1;
        }
    }
    return CallNextHookEx(ptrHook, nCode, wp, lp);
}
bool HasAltModifier(int flags)
{
    return (flags & 0x20) == 0x20;
}
4

1 回答 1

2

这并不像我想象的那么容易。但最后我设法做到了,解决方案非常简单。

编译后,您将使用古老的虚拟媒体键盘代码。 http://msdn.microsoft.com/en-us/library/dd375731%28v=VS.85%29.aspx

我将这个 cmd line prog 用于我的微软自然键盘,它没有媒体键,但有 5 个可编程键,要让它工作,你可以破解注册表

HKEY_CURRENT_USER\Software\Microsoft\IntelliType Pro\EventMapping\82 或

HKEY_CURRENT_USER\Software\Microsoft\IntelliType Pro\ModelSpecific\1016\EventMapping\82

其中 81 只是动态键之一 (78-​​82) 并将值放在 Arguments 键中。

using System;
using System.Runtime.InteropServices;

namespace NxtTrack
{    
class Program
{
    [DllImport("user32.dll")]
    private static extern bool PostMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll")]
    public static extern void keybd_event(byte vkCode, byte scanCode, int flags, IntPtr extraInfo);

    enum TrackMove
    {
        Previous,Next
    }

    static void Main(string[] args)
    {

        TrackMove trackMove;

        try
        {
            if(args[0].ToLower().Contains("previous"))
                trackMove = TrackMove.Previous;
            else if(args[0].ToLower().Contains("next"))
                trackMove = TrackMove.Next;
            else
            {
                throw new Exception("wrong param");
            }
        }
        catch
        {
            Console.WriteLine("Params needed: Next or Previous");
            return;
        }
        TrackKeys(trackMove);
    }

    private static void TrackKeys(TrackMove trackMove)
    {
        //http://msdn.microsoft.com/en-us/library/dd375731%28v=VS.85%29.aspx

        byte msg = trackMove == TrackMove.Previous ? (byte)0xB1 : (byte)0xB0;
        keybd_event(msg, 0x45, 0, IntPtr.Zero);
    }
}
}
于 2013-05-23T14:12:31.903 回答