简体   繁体   English

按下或释放键盘键时写入串行

[英]Write to serial when keyboard key is pressed or released

I am writing a Python script that will talk to an Arduino using serial. 我正在编写一个Python脚本,它将使用串行与Arduino对话。 The Arduino will implement a simple musical keyboard. Arduino将实现一个简单的音乐键盘。

So the desired behavior is: 因此,所需的行为是:

  • When I press a key on my PC keyboard, some byte is sent over serial; 当我按下PC键盘上的一个键时,一些字节通过串行发送。
  • While I keep the key pressed, nothing is sent; 当我按住键时,什么也没发送。
  • When I release that key, another given byte is sent. 当我释放该键时,将发送另一个给定的字节。

For single character input from the keyboard read this article . 对于从键盘输入的单个字符,请阅读本文

If you really want to get gritty, it's better to make a small GUI with pygtk or so. 如果您真的想变得坚韧不拔,最好使用pygtk左右制作一个小的GUI。 There you have easy access to keypress, keyrelease events. 在那里,您可以轻松访问按键,按键释放事件。 It can be done with very little code. 只需很少的代码即可完成。

On the serial port side, PySerial comes to mind - this makes it easier to change the baudrate etc. to adapt to the Arduino. 在串行端口方面,PySerial浮现在脑海-这使更改波特率等更容易以适应Arduino。

Are you trying to send MIDI commands? 您是否要发送MIDI命令?

import pygtk
import gtk

class MainWindow(gtk.Window):
    def __init__(self, debug = None):
        gtk.Window.__init__(self)
        self.connect("delete-event", self.on_delete_event)
        self.set_size_request(400, 300)

        bff = gtk.TextBuffer()
        self.txtview = gtk.TextView(bff)
        self.add(self.txtview)

        self.connect("key-press-event", self.on_key_press)
        self.connect("key-release-event", self.on_key_release)

    def on_key_press(self, key, event):
        # Send something
        print("Key pressed")
        pass

    def on_key_release(self, key, event):
        # Send something
        print("Key released")
        pass

    def on_delete_event(self, win, data):
        gtk.main_quit()

    def run(self):
        self.show_all()
        gtk.mainloop()



def main():
    # Do you PySerial initializing here
    w = MainWindow()
    w.run()
    return 0

if __name__ == '__main__':
    main()

暂无
暂无

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

相关问题 按下键时记录,释放键时停止 - record while key is pressed, stop when key is released Pygame,如果按键按下然后释放动作 - Pygame, if key pressed and then released action 在按下键时记录(获取)鼠标单击位置,并在python中释放相同的键时停止记录 - Record (get) mouse click position while key is pressed and stop recording when same key is released in python 如何在按下键时播放给定Hz的音调,并在释放时停止播放? - How do I play a tone of a given Hz while a key is pressed AND stop playing it when it is released? 仅在按下键盘键时向前或向后移动 4 个电机 - Move forward or backward 4 motors only when a keyboard key is pressed 每次按下并释放“ Space”键时,是否都可以进行屏幕截图? - Is there a way to take a screenshot everytime the key ''Space'' is pressed and released? 使 matplotlib 按钮小部件在按下时激活,而不是在释放时激活 - Make matplotlib button widget actuate when pressed, not when released 如何在按下和松开鼠标时存储鼠标位置? - How can I store the mouse position when it is pressed and when it is released? tkinter 在按钮保持按下状态时执行功能并在释放时停止 - tkinter executes function while the button stays pressed and stop when released 在pyqt4中按下修改键时,将所有键盘命令自动传送到嵌入式mplayer实例 - Pipe all keyboard commands automatically to an embedded mplayer instance when a modifier key is pressed in pyqt4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM