简体   繁体   English

C#UI多个KeyDown事件?? Windows窗体应用程序

[英]C# UI multiple KeyDown Events ?? windows form application

I am working on an mp3 player with minimal UI, in C# using windowsForm app. 我正在使用WindowsForm应用程序以C#在最小UI的mp3播放器上工作。

currently I have methods for open play and pause a song. 目前,我有公开播放和暂停歌曲的方法。

As I want the UI minimal, I only display the fileName of the playing song. 因为我希望UI最小化,所以我只显示正在播放的歌曲的文件名。

Now the problem is when I am trying to assign a KeyDown Event for each method. 现在的问题是,当我尝试为每种方法分配一个KeyDown事件时。

As all in my form I can select and map a KeyPress Event to is the Empty form, but it can only hold 1 KeyDown event, which now means I can only get 1 method to run.. 因为在我的表单中,我都可以选择并将KeyPress事件映射到为Empty表单,但是它只能容纳1个KeyDown事件,这意味着我只能运行1个方法。

How can I solve this ? 我该如何解决?

I assume I could use KeyPress and KeyUp events for my other 2 methods to run, but I suppose theres a more clever solution ? 我假设可以使用KeyPress和KeyUp事件来运行其他2种方法,但是我想有一个更聪明的解决方案?

part of my code: in this example, OpenFile works... but Play doesnt work at the same time, because I havent assigned any EventHandler for it, I guess. 我的代码的一部分:在此示例中,OpenFile可以工作...但是Play不能同时工作,因为我想还没有为其分配任何EventHandler。 But as I dont want any button in the UI.,, What can I then do ?? 但是,由于我不想在UI中使用任何按钮,所以该怎么办?

I dont want a traditional shortcut command either. 我也不想要传统的快捷方式命令。 like ctrl + key. 像Ctrl +键。

private void o(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.O)
            {
                openFileDialog1.ShowDialog();
            }
        }

        private void p(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.P)
            {
                player.Play();
            }
        }

Simply 只是

private void o(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.O)
        {
            openFileDialog1.ShowDialog();
        }

        else if (e.KeyCode == Keys.P)
        {
            player.Play();
        }
    }

you may want to define only one event-handler, but you can check for several keys pressed. 您可能只想定义一个事件处理程序,但是可以检查是否按下了几个键。

I'd personally put it into a case statement and into a single method/function. 我个人将其放入案例声明和单个方法/函数中。 This way you can expand without a web of if/thens. 这样,您可以在没有if / thens网络的情况下进行扩展。

private void o(object sender, KeyEventArgs e) {
switch (e.KeyCode) {
    case Keys.O: 
        openFileDialog1.ShowDialog();
        break;
    case Keys.P:
        player.Play();
        break;
    default: 
        break;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM