跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 求助 Python 监听大写锁定键
未分類
21 7 月 2020

求助 Python 监听大写锁定键

求助 Python 监听大写锁定键

資深大佬 : JackalZhao 6

需求:

持续监听大写锁定键,当按下后,保持按下状态超过 0.5 秒时,立即 print(‘超过 0.5 秒’)

(一旦在持续 0.5 秒之前松开了,那就啥都不做)

当按下超过 0.5 秒后,松开这个按键时,print(‘松开了’)。

尽可能简洁、占用资源尽可能少。

大佬有話說 (10)

  • 資深大佬 : singerll

    先报价

  • 資深大佬 : loading

    caps lck 键有按下释放事件吗?容我有空的时候去看看。

  • 資深大佬 : manning

    keyboard hook windows api

  • 資深大佬 : hakono

    这不是 Python 编程知识,而是 win32 开发知识,请用 c 或 c++来写
    这里涉及到 Windows 的全局 hook 相关的 api 以及 Windows 的消息事件
    这个基础需要去学一下的
    https://zhuanlan.zhihu.com/p/31962703
    以及,全局 hook 必须需要你编译一个 dll 用来注入,这就是为什么让你用 c 或 c++来写,了解之后实现这个功能非常简单几十行代码就行了

  • 資深大佬 : rming

    推荐 2 个库 pynput keyboard

  • 資深大佬 : tabris17

    用底层键盘消息钩子就可以了

    给 LZ 两个关键词
    SetWindowsHookEx
    和
    WH_KEYBOARD_LL

    自己搜去吧

  • 主 資深大佬 : JackalZhao

    用 keyboard 和 thread 解决了。

  • 資深大佬 : mutalisk

    pyhook

  • 資深大佬 : ysc3839

    @hakono “全局 hook 必须需要你编译一个 dll 用来注入”是错的。比如 WH_KEYBOARD_LL 和 WH_MOUSE_LL 是全局的但不需要,微软文档里有写:
    https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644985(v=vs.85)
    However, the WH_KEYBOARD_LL hook is not injected into another process. Instead, the context switches back to the process that installed the hook and it is called in its original context.

    The Old New Thing 里面也有一篇文章提到了这个:
    https://devblogs.microsoft.com/oldnewthing/20050425-41/?p=35803
    The WH_KEYBOARD_LL and WH_MOUSE_LL hooks are exceptions to this rule. These two are non-injecting hooks, as explained in their respective documentation pages. Rather, the hook function is called in its original thread context.

  • 資深大佬 : hakono

    @ysc3839 原来如此,这两是例外啊。一直都是搞个 dll 写的。。。。

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具