本文整理汇总了Python中pynput.keyboard.Listener方法的典型用法代码示例。如果您正苦于以下问题:Python keyboard.Listener方法的具体用法?Python keyboard.Listener怎么用?Python keyboard.Listener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pynput.keyboard
的用法示例。
在下文中一共展示了keyboard.Listener方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def main():
# prepare audio recorder
p = pyaudio.PyAudio()
stream = p.open(
format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
stream_callback=callback)
stream.start_stream()
# prepare keyboard listener
with keyboard.Listener(
on_press=on_press, on_release=on_release) as listener:
listener.join()
# close up
stream.stop_stream()
stream.close()
p.terminate()
示例2: windows_keys_gui
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def windows_keys_gui():
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Alt" + color.END + " key to confirm the new keymapping to Ctrl..")
with Listener(
# on_press=on_press,
on_release=is_ctrl_winchrome) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Win/Super" + color.END + " key to confirm the new keymapping to Alt..")
with Listener(
# on_press=on_press,
on_release=is_alt_windows) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Ctrl" + color.END + " key to confirm the new keymapping to Win/Super..")
with Listener(
# on_press=on_press,
on_release=is_super_winmac) as listener:
listener.join()
示例3: init_controls
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def init_controls(self):
"""Define keys and add listener"""
self.controls = {
'w': 'forward',
's': 'backward',
'a': 'left',
'd': 'right',
'Key.space': 'up',
'Key.shift': 'down',
'Key.shift_r': 'down',
'q': 'counter_clockwise',
'e': 'clockwise',
'i': lambda speed: self.drone.flip_forward(),
'k': lambda speed: self.drone.flip_back(),
'j': lambda speed: self.drone.flip_left(),
'l': lambda speed: self.drone.flip_right(),
# arrow keys for fast turns and altitude adjustments
'Key.left': lambda speed: self.drone.counter_clockwise(speed),
'Key.right': lambda speed: self.drone.clockwise(speed),
'Key.up': lambda speed: self.drone.up(speed),
'Key.down': lambda speed: self.drone.down(speed),
'Key.tab': lambda speed: self.drone.takeoff(),
'Key.backspace': lambda speed: self.drone.land(),
'p': lambda speed: self.palm_land(speed),
't': lambda speed: self.toggle_tracking(speed),
'r': lambda speed: self.toggle_recording(speed),
'z': lambda speed: self.toggle_zoom(speed),
'Key.enter': lambda speed: self.take_picture(speed)
}
self.key_listener = keyboard.Listener(on_press=self.on_press,
on_release=self.on_release)
self.key_listener.start()
# self.key_listener.join()
示例4: chromebook_keys_gui
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def chromebook_keys_gui():
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Alt" + color.END + " key to confirm the new keymapping to Ctrl..")
with Listener(
# on_press=on_press,
on_release=is_ctrl_winchrome) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Ctrl" + color.END + " key to confirm the new keymapping to Alt..")
with Listener(
# on_press=on_press,
on_release=is_alt_chromebook) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Search" + color.END + " key to confirm the new keymapping to Super/Win..")
with Listener(
# on_press=on_press,
on_release=is_super_chromebook) as listener:
listener.join()
示例5: chromebook_keys_terminal
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def chromebook_keys_terminal():
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Alt" + color.END + " key to confirm the new keymapping to Super/Win..")
with Listener(
# on_press=on_press,
on_release=is_super_terminal) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Ctrl" + color.END + " key to confirm the new keymapping to Ctrl..")
with Listener(
# on_press=on_press,
on_release=is_ctrl_terminal) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Search" + color.END + " key to confirm the new keymapping to Alt..")
with Listener(
# on_press=on_press,
on_release=is_alt_chromebook_terminal) as listener:
listener.join()
示例6: windows_keys_terminal
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def windows_keys_terminal():
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Alt" + color.END + " key to confirm the new keymapping to Super/Win..")
with Listener(
# on_press=on_press,
on_release=is_super_terminal) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Win/Super" + color.END + " key to confirm the new keymapping to Alt..")
with Listener(
# on_press=on_press,
on_release=is_alt_windows) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Ctrl" + color.END + " key to confirm the new keymapping to Ctrl..")
with Listener(
# on_press=on_press,
on_release=is_ctrl_terminal) as listener:
listener.join()
示例7: mac_keys_gui
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def mac_keys_gui():
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Command" + color.END + " key to confirm the new keymapping to Ctrl..")
with Listener(
# on_press=on_press,
on_release=is_ctrl_mac) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Alt" + color.END + " key to confirm it remains Alt..")
with Listener(
# on_press=on_press,
on_release=is_alt_mac) as listener:
listener.join()
print()
print(color.UNDERLINE + color.YELLOW + "Press the physical Ctrl" + color.END + " key to confirm the new keymapping to Win/Super..")
with Listener(
# on_press=on_press,
on_release=is_super_winmac) as listener:
listener.join()
示例8: flight_data_handler
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def flight_data_handler(self, event, sender, data):
"""Listener to flight data from the drone."""
text = str(data)
if self.prev_flight_data != text:
self.prev_flight_data = text
示例9: listen
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def listen():
listener = keyboard.Listener(on_press = on_press,
on_release = on_release)
listener.start()
示例10: run
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def run(self):
with keyboard.Listener(
on_press=self.on_press,
) as listener:
listener.join()
示例11: listen_keyboard
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def listen_keyboard():
data_path = 'Data/Train_Data/Keyboard'
if not os.path.exists(data_path):
os.makedirs(data_path)
def on_press(key):
save_event_keyboard(data_path, 1, key)
def on_release(key):
save_event_keyboard(data_path, 2, key)
with key_listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
示例12: flight_data_handler
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def flight_data_handler(self, event, sender, data):
"""
Listener to flight data from the drone.
"""
self.battery = data.battery_percentage
self.fly_mode = data.fly_mode
self.throw_fly_timer = data.throw_fly_timer
self.throw_ongoing = data.throw_fly_timer > 0
# print("fly_mode",data.fly_mode)
# print("throw_fly_timer",data.throw_fly_timer)
# print("em_ground",data.em_ground)
# print("em_sky",data.em_sky)
# print("electrical_machinery_state",data.electrical_machinery_state)
#print("em_sky",data.em_sky,"em_ground",data.em_ground,"em_open",data.em_open)
#print("height",data.height,"imu_state",data.imu_state,"down_visual_state",data.down_visual_state)
if self.is_flying != data.em_sky:
self.is_flying = data.em_sky
log.debug(f"FLYING : {self.is_flying}")
if not self.is_flying:
self.reset()
else:
if self.tracking_after_takeoff:
log.info("Tracking on after takeoff")
self.toggle_tracking(True)
log.debug(f"MODE: {self.fly_mode} - Throw fly timer: {self.throw_fly_timer}")
示例13: log_data_handler
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def log_data_handler(self, event, sender, data):
"""
Listener to log data from the drone.
"""
pos_x = -data.mvo.pos_x
pos_y = -data.mvo.pos_y
pos_z = -data.mvo.pos_z
if abs(pos_x)+abs(pos_y)+abs(pos_z) > 0.07:
if self.ref_pos_x == -1: # First time we have meaningful values, we store them as reference
self.ref_pos_x = pos_x
self.ref_pos_y = pos_y
self.ref_pos_z = pos_z
else:
self.pos_x = pos_x - self.ref_pos_x
self.pos_y = pos_y - self.ref_pos_y
self.pos_z = pos_z - self.ref_pos_z
qx = data.imu.q1
qy = data.imu.q2
qz = data.imu.q3
qw = data.imu.q0
self.yaw = quat_to_yaw_deg(qx,qy,qz,qw)
if self.write_log_data:
if self.write_header:
self.log_file.write('%s\n' % data.format_cvs_header())
self.write_header = False
self.log_file.write('%s\n' % data.format_cvs())
示例14: main
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def main():
switcher = Switcher()
with keyboard.Listener(
on_press=switcher.on_press,
on_release=switcher.on_release) as listener:
listener.join()
示例15: _start
# 需要导入模块: from pynput import keyboard [as 别名]
# 或者: from pynput.keyboard import Listener [as 别名]
def _start(self):
with Listener(on_press=self.on_press, on_release=self.on_release) as self.listener:
self.listener.join()