Code Monkey home page Code Monkey logo

qtglobalevent's Introduction

个人主页-CSDN-orange国内站点-gitee-orange国外站点-github-brightgreen

这是一个使用Qt封装的全局鼠标、键盘事件监听器的库

功能 系统 是否实现 实现方式
全局鼠标事件监听器 Windows 已实现 user32鼠标钩子
全局鼠标事件监听器 Linux 已实现 x11 API
全局键盘事件监听器 Windows 已实现 user32键盘钩子
全局键盘事件监听器 Linux 暂未实现 x11 xcb API
  • 备注:Linux下全局键盘事件监听器通过使用X11 API已经获取到了全局键盘事件,但是不知道怎么将X11键盘事件映射为QKeyEvent,通过阅读Qt源码,发现Qt使用的是xcb将xkb映射为QKeyEvent,后续有时间在继续研究(有懂的朋友欢迎补充,或者联系我:[email protected]
  • Linux键盘事件映射QKeyEvent源码位置:D:\Qt\Qt5.12.5\5.12.5\Src\qtbase\src\plugins\platforms\xcb\qxcbkeyboard.cpp

实现效果

GlobalMouseKey

使用方式

  • 新建一个工程,在pro文件中引入模块

    include($$PWD/GlobalMouseKey/GlobalMouseKey.pri)
    INCLUDEPATH += $$PWD/GlobalMouseKey
  • 在需要使用到全局鼠标、键盘事件的类中添加头文件

    #include <globalmouseevent.h>
    #include "globalkeyevent.h"
  • 绑定对应的信号

    connect(GlobalMouseEvent::getInstance(), &GlobalMouseEvent::mouseEvent, this, &Widget::on_mouseEvent);
    connect(GlobalMouseEvent::getInstance(), &GlobalMouseEvent::wheelEvent, this, &Widget::on_wheelEvent);
    connect(GlobalKeyEvent::getInstance(), &GlobalKeyEvent::keyEvent, this, &Widget::on_keyEvent);
  • 安装全局鼠标、键盘事件监听器后就可以使用了

    /**
     * @brief 安装全局鼠标事件监听器
     */
    void Widget::on_but_mouseI_clicked()
    {
        bool ret = GlobalMouseEvent::installMouseEvent();
        ui->textEdit->append(QString("<<<<<<<<<< 全局鼠标事件监听器安装%1 >>>>>>>>>>").arg(ret ? "成功" : "失败"));
    }
    
    /**
     * @brief 卸载全局鼠标事件监听器
     */
    void Widget::on_but_mouser_clicked()
    {
        bool ret = GlobalMouseEvent::removeMouseEvent();
        ui->textEdit->append(QString("<<<<<<<<<< 全局鼠标事件监听器卸载%1 >>>>>>>>>>").arg(ret ? "成功" : "失败"));
    }
    
    /**
     * @brief 安装全局键盘事件监听器
     */
    void Widget::on_but_keyI_clicked()
    {
        bool ret = GlobalKeyEvent::installKeyEvent();
        ui->textEdit->append(QString("<<<<<<<<<< 全局键盘事件监听器安装%1 >>>>>>>>>>").arg(ret ? "成功" : "失败"));
    }
    
    /**
     * @brief 卸载全局键盘事件监听器
     */
    void Widget::on_but_KeyR_clicked()
    {
        bool ret = GlobalKeyEvent::removeKeyEvent();
        ui->textEdit->append(QString("<<<<<<<<<< 全局键盘事件监听器卸载%1 >>>>>>>>>>").arg(ret ? "成功" : "失败"));
    }
    
  • 注意:每个信号建议只绑定一次,接收到信号后由于是指针,在使用完成后记得delete

qtglobalevent's People

Contributors

mahuifa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.