Wxpython 键盘绑定 [英] Wxpython Keyboard binding

查看:48
本文介绍了Wxpython 键盘绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究节奏训练器,并使用 wxpython 作为 UI 工具包.我想知道是否有人知道如何绑定键盘按键来播放声音?所以简单地说,用户可以使用键盘来演奏鼓点.示例按 A 键将播放低音鼓"

So I'm working on a rhythm trainer, and using wxpython as the UI Toolkit. I was wondering if anyone knew how to bind keyboard presses to play sounds? So to put it simply, users can use the keyboard to play a drum beat. Example "Pressing the A key will play the bass drum"

现在我遇到了一个教程 -

Now I've come across a tutorial -

http://www.blog.pythonlibrary.org/2009/08/29/wxpython-catch-key-and-char-events/

但这似乎需要按钮才能成功播放声音.我有一些使用这个例子的功能.但我想知道是否有另一种无需按钮即可完成的方法?

But this seems like it needs the button to successfully play the sound. I've got a bit of the functionality working using this example. But I was wondering if there's another way to do it without the need of a button?

import wx

class MyForm(wx.Frame):


def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "Key Press Tutorial")

    # Add a panel so it looks the correct on all platforms
    panel = wx.Panel(self, wx.ID_ANY)
    btn = wx.Button(panel, label="OK")

    btn.Bind(wx.EVT_KEY_DOWN, self.onKeyPress)

def onKeyPress(self, event):
    keycode = event.GetKeyCode()
    print keycode
    if keycode == ord('A'):
        print "you pressed the spacebar!"
        sound_file = "notation1.wav"
        sound=wx.Sound(sound_file)
        print(sound_file)
        sound.Play(wx.SOUND_ASYNC)
    event.Skip()

 # Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm()
    frame.Show()
    app.MainLoop()

这是我使用教程的示例.

This is my example using the tutorial.

干杯!

推荐答案

我的教程是关于捕捉焦点小部件上的按键.在本教程的情况下,这是焦点按钮,这就是它绑定到 EVT_KEY_DOWN 的原因.遗憾的是,面板并不是真的很容易接受焦点,因此您最好使用 SetFocus() 手动将焦点设置在面板上,或者将关键事件绑定到大多数小部件.

My tutorial is about catching keystrokes on the widget that's in focus. In the case of the tutorial, that was the button that was in focus and that was why it was bound to EVT_KEY_DOWN. Sadly, panels don't really accept focus very easily, so you'll be better off to set the focus on the panel manually using SetFocus() or bind the key event to most of the widgets.

您或许可以使用 AcceleratorTable,但我不确定这是否适用于您的情况.这里有一个关于该主题的教程的链接:

You might be able to use an AcceleratorTable, but I'm not sure if that will work in your situation. Here's a link to a tutorial on that topic though:

http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/

这篇关于Wxpython 键盘绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆