IOError:[Errno 1]不允许操作:' / dev / uinput'

时间:2017-07-03 15:00:18

标签: macos python-2.7 keyboard keyevent pyaudio

我似乎有一些问题让python读取键事件,我写了这段代码 当我有空间时录音,当我释放它时停止...

import pyaudio
import wave
import keyboard

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

while keyboard.is_pressed('space'):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

但是在运行此代码时,我收到此错误消息。

python sound_record.py 
* recording
Traceback (most recent call last):
  File "sound_record.py", line 24, in <module>
    while keyboard.is_pressed('space'):
  File "/usr/local/lib/python2.7/site-packages/keyboard/__init__.py", line 162, in is_pressed
    _listener.start_if_necessary()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_generic.py", line 36, in start_if_necessary
    self.init()
  File "/usr/local/lib/python2.7/site-packages/keyboard/__init__.py", line 112, in init
    _os_keyboard.init()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_nixkeyboard.py", line 110, in init
    build_device()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_nixkeyboard.py", line 106, in build_device
    ensure_root()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_nixcommon.py", line 163, in ensure_root
    raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.

当我使用sudo时:

sudo !!
sudo python sound_record.py 
Password:
* recording
Traceback (most recent call last):
  File "sound_record.py", line 24, in <module>
    while keyboard.is_pressed('space'):
  File "/usr/local/lib/python2.7/site-packages/keyboard/__init__.py", line 162, in is_pressed
    _listener.start_if_necessary()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_generic.py", line 36, in start_if_necessary
    self.init()
  File "/usr/local/lib/python2.7/site-packages/keyboard/__init__.py", line 112, in init
    _os_keyboard.init()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_nixkeyboard.py", line 110, in init
    build_device()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_nixkeyboard.py", line 107, in build_device
    device = aggregate_devices('kbd')
  File "/usr/local/lib/python2.7/site-packages/keyboard/_nixcommon.py", line 141, in aggregate_devices
    uinput = make_uinput()
  File "/usr/local/lib/python2.7/site-packages/keyboard/_nixcommon.py", line 27, in make_uinput
    uinput = open("/dev/uinput", 'wb')
IOError: [Errno 1] Operation not permitted: '/dev/uinput'

为什么我收到此错误消息?

1 个答案:

答案 0 :(得分:1)

您似乎正在使用其description所在的Python包keyboard

  

在Windows和Linux上挂钩并模拟键盘事件

如果你想在MacOS上使用键盘事件,你需要找到一个能够做到这一点的软件包。