dimas 2021-06-27 18:42 采纳率: 100%
浏览 42
已结题

c#窗口接收usb midi按键?

我买了1个电子琴做生日礼物。

卖琴的第一个月免费,以后每月30元。

所以我想自己弄一个。。

c#窗口接收按键

private void Form1_KeyPress(object sender, KeyPressEventArgs e)

{

if((Keys)e.KeyChar==Keys.Enter)

{

MessageBox.Show("enter");

}

}

 

请问大哥,怎么接收电子琴按键,感谢大佬,其他地方找的都是控制台。。

 

我把那个控制台程序复制出来新建成了窗口程序。调试时终于在输出窗口看到了信息。

它的输出方法是public void MidiProc(IntPtr hMidiIn,
                uint wMsg,
                IntPtr dwInstance,
                UInt32 dwParam1,
                UInt32 dwParam2)

我在里面添加了全局变量,用一个100的时钟去读取。加了个开关BBB。

这里有个问题就是同时按下10多个按键时钟应该会遗漏。是否应该改成队列。

或者另外 一个什么Proc。。让它自动显示出来,研究2周了,大哥来源码。。非常感谢!

得到的按键名称是为了,按着要做瀑布落下来同时比较4,5个按键了..

        static uint midi_wMsg11;
        static UInt32 midi_dw11;
        static UInt32 mide_dw11;
        static UInt32 dwParam11;
        static UInt32 dwParam21;

        static uint BBB = 0;
        static uint ccc = 0;

 

新建了个窗口,一个时钟100显示到textbox1,

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;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }





        public class InputPort
        {
            private NativeMethods.MidiInProc midiInProc;
            private IntPtr handle;

            public InputPort()
            {
                midiInProc = new NativeMethods.MidiInProc(MidiProc);
                handle = IntPtr.Zero;
            }

            public static int InputCount
            {
                get { return NativeMethods.midiInGetNumDevs(); }
            }

            public bool Close()
            {
                bool result = NativeMethods.midiInClose(handle)
                    == NativeMethods.MMSYSERR_NOERROR;
                handle = IntPtr.Zero;
                return result;
            }

            public bool Open(int id)
            {
                return NativeMethods.midiInOpen(
                    out handle,
                    id,
                    midiInProc,
                    IntPtr.Zero,
                    NativeMethods.CALLBACK_FUNCTION)
                        == NativeMethods.MMSYSERR_NOERROR;
            }

            public bool Start()
            {
                return NativeMethods.midiInStart(handle)
                    == NativeMethods.MMSYSERR_NOERROR;
            }

            public bool Stop()
            {
                return NativeMethods.midiInStop(handle)
                    == NativeMethods.MMSYSERR_NOERROR;
            }

            public IntPtr hmidi = IntPtr.Zero;
            public uint midi_wMsg = 0;
            public IntPtr midi_dwIns = IntPtr.Zero;
            public UInt32 midi_dw1 = 0;
            public UInt32 mide_dw2 = 0;


            public void MidiProc(IntPtr hMidiIn,
                uint wMsg,
                IntPtr dwInstance,
                UInt32 dwParam1,
                UInt32 dwParam2)
            {
                // Receive messages here
                hmidi = hMidiIn;

                if (wMsg == 963)
                {
                    dwParam1 = dwParam1 & 0xFFFF;
                    uint h_dw1 = 0;
                    uint l_dw1 = 0;
                    h_dw1 = dwParam1 & 0xFF;
                    l_dw1 = (dwParam1 >> 8) & 0xFF;

                    Console.WriteLine(Convert.ToString(wMsg, 16));
                    Console.WriteLine(Convert.ToString(h_dw1, 16));
                    Console.WriteLine(Convert.ToString(l_dw1, 16));
                    Console.WriteLine(Convert.ToString(dwParam2, 16));
                    Console.WriteLine("-------------------------------");
                    midi_dw11 = h_dw1;
                    mide_dw11 = l_dw1;
                }
                else
                {
                    Console.WriteLine(Convert.ToString(wMsg, 16));
                    Console.WriteLine(Convert.ToString(dwParam1, 16));
                    Console.WriteLine(Convert.ToString(dwParam2, 16));
                    Console.WriteLine("-------------------------------");
                    dwParam11 = dwParam1;
                }
                midi_wMsg11 = wMsg;

                dwParam21 = dwParam2;

                BBB = 1;
            }
        }

        internal static class NativeMethods
        {
            internal const int MMSYSERR_NOERROR = 0;
            internal const int CALLBACK_FUNCTION = 0x00030000;

            internal delegate void MidiInProc(
                IntPtr hMidiIn,
                uint wMsg,
                IntPtr dwInstance,
                UInt32 dwParam1,
                UInt32 dwParam2);

            [DllImport("winmm.dll")]
            internal static extern int midiInGetNumDevs();

            [DllImport("winmm.dll")]
            internal static extern int midiInClose(
                IntPtr hMidiIn);

            [DllImport("winmm.dll")]
            internal static extern int midiInOpen(
                out IntPtr lphMidiIn,
                int uDeviceID,
                MidiInProc dwCallback,
                IntPtr dwCallbackInstance,
                int dwFlags);

            [DllImport("winmm.dll")]
            internal static extern int midiInStart(
                IntPtr hMidiIn);

            [DllImport("winmm.dll")]
            internal static extern int midiInStop(
                IntPtr hMidiIn);
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
        static uint midi_wMsg11;
        static UInt32 midi_dw11;
        static UInt32 mide_dw11;
        static UInt32 dwParam11;
        static UInt32 dwParam21;

        static uint BBB = 0;
        static uint ccc = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            InputPort ip = new InputPort();
            int i = InputPort.InputCount;
            bool isOpen = false;
            bool isStart = false;
            isOpen = ip.Open(i - 1);
            if (isOpen == true)
            {
                isStart = ip.Start();
            }
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (BBB == 1)
            {
                BBB = 0;
                if (ccc > 20) { textBox1.Text = ""; ccc = 0; }
                textBox1.Text = textBox1.Text + System.Environment.NewLine + "midi_wMsg:" + Convert.ToString(midi_wMsg11, 16);
                textBox1.Text = textBox1.Text + System.Environment.NewLine + "dwParam1:" + Convert.ToString(dwParam11, 16);
                textBox1.Text = textBox1.Text + System.Environment.NewLine + "dwParam2:" + Convert.ToString(dwParam21, 16);
                textBox1.SelectionStart = textBox1.Text.Length;
                textBox1.ScrollToCaret();
                ccc = ccc + 1;
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 系统已结题 11月25日
  • 已采纳回答 11月17日

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能