当前位置: 首页>>代码示例>>Python>>正文


Python Keyboard.on方法代码示例

本文整理汇总了Python中keyboard.Keyboard.on方法的典型用法代码示例。如果您正苦于以下问题:Python Keyboard.on方法的具体用法?Python Keyboard.on怎么用?Python Keyboard.on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在keyboard.Keyboard的用法示例。


在下文中一共展示了Keyboard.on方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Widget

# 需要导入模块: from keyboard import Keyboard [as 别名]
# 或者: from keyboard.Keyboard import on [as 别名]
class Widget(QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent, Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(self.windowFlags() | Qt.Tool)
        self.resize(300, 30)
        self.cmd = []
        self.lastCmd = ''
        self.cmds = [
            '!yx', '!quit', 'dt', 'dh', 'cmd', 'av', 'put',
            'rm', 'mt', 'bs', 'ba', 'te',
        ]
        self.cmds += [f.split('.')[0] for f in os.listdir(hotkeys)]
        print 'cmds: '
        print self.cmds
        self.keyboard = Keyboard()
        self.keyboard.on('ctrl ;', self.toggle_active)
        self.keyboard.on('print', self.screenshot)

    def toggle_active(self):
        if self.isVisible():
            self.clear()
        else:
            self.show()
            self.setWindowState(Qt.WindowMinimized)
            self.setWindowState(Qt.WindowActive)

            desktop = QDesktopWidget()
            height = desktop.geometry().height()

            pos = self.pos()
            self.move(
                pos.x(), height - self.height() - 50)

    def screenshot(self):
        im = ImageGrab.grab()
        ts = datetime.strftime(datetime.now(), config.FNAME_TIMESTAMP_FORMAT)
        fpath = os.path.join(SCREENSHOTS_PATH, ts + '.png')
        if im:
            im.save(fpath, 'png')

    def keyPressEvent(self, event):
        ch = event.text()
        # ignore ctrl-; (although this will ignore all ;)
        if not ch or ch ==';':
            return
        # backspace
        if ch == '\b':
            if self.cmd:
                self.cmd.pop()
                self.update()
        # esc | ctrl-k
        elif ch == '\x1b' or ch == '\x0b':
            del self.cmd[:]
            self.update()
        # return | ctrl-j | ctrl-m
        elif ch and ch in '\r\n':
            text = self.text()
            if not text or text in self.cmds:
                self.execute()
        # normal char
        else:
            self.cmd.append(ch)
            self.update()
            if self.matched():
                self.execute()
            else:
                print 'no match'
        print 'current cmd: {}'.format(u''.join(self.cmd))

    def matched(self):
        matches = []
        text = self.text()
        for cmd in self.cmds:
            if cmd.startswith('!') and cmd[1:] == text:
                matches.append(text)
            elif cmd.startswith(text):
                matches.append(cmd)
        if len(matches) == 1 and text == matches[0]:
            self.cmd = matches[0]
            return True
        else:
            return False

    def text(self):
        # eliminate initial semicolon
        return ''.join(self.cmd)

    def execute(self):
        # hide quick-console window before cmd is executed
        self.hide()
        cmd = self.text()
        if not cmd:
            cmd = self.lastCmd
        # yinxiang
        if cmd == 'yx':
            command('start chrome "https://app.yinxiang.com/Home.action"')
        # date time in filename format
        elif cmd == 'dt':
            copyToClipboard(curDatetime('%Y%m%d%H%M%S'))
#.........这里部分代码省略.........
开发者ID:fans656,项目名称:quick-console,代码行数:103,代码来源:main.py


注:本文中的keyboard.Keyboard.on方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。