QT项目之键盘控制光标移动
今天学习了QT类中的键盘和鼠标事件,就写了一个很简单的应用程序来做测试。突然发现用C++开发项目效率好高,什么都可以调用类,或者去继承某一个类,添加自己的函数就行。以前做图形界面和鼠标开发的时候,全是自己造轮子,用C语言开发,自己去读取/dev/input/event*接口,然后去处理获取到的input数据,那时候感觉好复杂,现在用QT直接一个类就搞定,界面效果如下:
屏幕中间的指针只能在坐标轴上移动,用上下左右键控制方向,有一个checkBox可选按钮来控制光标是否可以连续移动代码如下:
main.cpp
#include <QtGui/QApplication>
#include "widget.h"int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
widget.h /* 有四个变量没有用到,分别是上下左右标志位,留着扩展 */
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QKeyEvent>
#include <QDebug>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;bool keyUp;bool keyDown;bool keyLeft;bool keyRight;bool flag;protected:void keyPressEvent(QKeyEvent *event);void keyReleaseEvent(QKeyEvent *event);
};#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPalette>
#include <QDebug>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{QPalette pa;ui->setupUi(this);pa.setColor(QPalette::WindowText,Qt::red);ui->label->setPalette(pa);//setFocus();flag=1; /* 看是否使用isAutoRepeat */keyUp=false;keyLeft=false;keyRight=false;keyDown=false;ui->pushButton->move(180,130); /* set middle point */
}Widget::~Widget()
{delete ui;
}void Widget::keyPressEvent(QKeyEvent *event)
{
}void Widget::keyReleaseEvent(QKeyEvent *event)
{static int offset_x=0,offset_y=0;flag =ui->checkBox->isChecked();if(event->key() == Qt::Key_Up){if(event->isAutoRepeat()&&(flag==0)) return;if(offset_x==0){offset_y=offset_y-10;if(offset_y<-100)offset_y=-100;ui->pushButton->move(180+offset_x,130+offset_y);}}if(event->key() == Qt::Key_Down){if(event->isAutoRepeat()&&(flag==0)) return;if(offset_x==0){offset_y=offset_y+10;if(offset_y>100)offset_y=100;ui->pushButton->move(180+offset_x,130+offset_y);}}if(event->key() == Qt::Key_Left){if(event->isAutoRepeat()&&(flag==0)) return;if(offset_y==0){offset_x=offset_x-10;if(offset_x<-100)offset_x=-100;ui->pushButton->move(180+offset_x,130+offset_y);}}if(event->key() == Qt::Key_Right){if(event->isAutoRepeat()&&(flag==0)) return;if(offset_y==0){offset_x=offset_x+10;if(offset_x>100)offset_x=100;ui->pushButton->move(180+offset_x,130+offset_y);}}
}
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>400</width><height>300</height></rect></property><property name="windowTitle"><string>Widget</string></property><widget class="Line" name="line"><property name="geometry"><rect><x>80</x><y>130</y><width>200</width><height>16</height></rect></property><property name="orientation"><enum>Qt::Horizontal</enum></property></widget><widget class="Line" name="line_2"><property name="geometry"><rect><x>180</x><y>30</y><width>16</width><height>200</height></rect></property><property name="orientation"><enum>Qt::Vertical</enum></property></widget><widget class="QPushButton" name="pushButton"><property name="geometry"><rect><x>80</x><y>130</y><width>16</width><height>16</height></rect></property><property name="text"><string>o</string></property></widget><widget class="QCheckBox" name="checkBox"><property name="geometry"><rect><x>270</x><y>240</y><width>81</width><height>21</height></rect></property><property name="text"><string>speed</string></property></widget><widget class="QLabel" name="label"><property name="geometry"><rect><x>100</x><y>0</y><width>161</width><height>21</height></rect></property><property name="text"><string>光标在数轴上移动演示</string></property><property name="alignment"><set>Qt::AlignCenter</set></property></widget></widget><layoutdefault spacing="6" margin="11"/><resources/><connections/>
</ui>
QT项目之键盘控制光标移动相关推荐
- 电脑技巧——用键盘控制光标
文章目录 需求 实现 基本功能 需求 写code时常常发现,输入[会自动补全],然而光标却在]外面,这是一个非常尴尬的问题, 如果需要调整鼠标光标进入[]内,就需要移动鼠标,本来手在键盘上,现在需要去 ...
- mfc键盘控制移动鼠标光标_如何在Windows中使用键盘控制鼠标光标
mfc键盘控制移动鼠标光标 If you're stuck without a mouse temporarily, don't worry-you can still use your PC. Yo ...
- c++用键盘按键控制光标移动_如何在Windows10中用键盘控制鼠标光标
如果你的鼠标突然坏掉了,此时找不到替代鼠标,或者你使用的鼠标是电池供电的,此时电量耗尽,那么你可以将键盘配置为鼠标暂时缓解燃眉之急. 设置步骤 键盘鼠标控制功能内置于Windows10中.首先进入设置 ...
- win10 java模拟鼠标_[pc玩家]如何在Windows10中用键盘控制鼠标光标
如果你的鼠标突然坏掉了,此时找不到替代鼠标,或者你使用的鼠标是电池供电的,此时电量耗尽,那么你可以将键盘配置为鼠标暂时缓解燃眉之急. 设置步骤 键盘鼠标控制功能内置于Windows10中.首先进入设置 ...
- VR项目添加键盘鼠标控制
VR项目添加键盘鼠标控制 使用VR模版创建的项目,程序运行后,只能使用VR头盔进行交互场景,不便于调试工作,为此,在场景中添加键盘.鼠标控制功能. 1 .键盘控制添加: 键盘控制主要为ASDW 键控制 ...
- 润乾键盘控制填报光标移动
A:润乾报表提供了强有力的填报功能,支持多表回填.来去无关,从而比起简单的单表直接填报更能适应复杂的业务需求. B:在填报表中我们通常可能会利用鼠标来定位具体的单元格,然后进行填报操作, 但对于一些 ...
- QT入门第七天 网络编程TCP/IP/UDP+Http和JSON解析+qt事件软键盘【CSDN最详细】
网络编程+Http和JSON解析+qt事件软键盘 第一章 QT中的网络编程 [1]涉及到的类 [2]tcp协议的流程 [2.1]服务器的流程 socket-->bind-->listen- ...
- 【Qt】QT鼠标和键盘事件
00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. 鼠标事件 04. 键盘事件 05. 附录 01. 概述 事件是对各种应用程序需要知道的由应用程序内部或者外部产生的事情或者 ...
- QT 录制鼠标键盘回放
QT 录制鼠标键盘回放 前段时间因为朋友想要解放双手想要把一些操作录制回放 所以做了这个小项目 因为我目前运用的QT只能在程序中去记录鼠标跟键盘的操作 所以网上找了找方法 有钩子函数使用 所以运用了钩 ...
最新文章
- linux下后台执行shell脚本
- Oracle 常见的33个等待事件
- Merge Two Sorted Lists
- UA MATH636 信息论6 微分熵
- lda主题评论文本python_利用python做LDA文本分析,该从哪里入手呢?
- 工业物联网的应用领域和方向
- 洛谷P3195 [HNOI2008]玩具装箱TOY(单调队列优化DP)
- 【51单片机快速入门指南】1:基础知识和工程创建
- Redis windows学习(二)——Redis持久化的AOF模式和RDB模式
- 互联网架构设计漫谈 (6)-90%的架构师都知道的工作流原理
- 听同事讲 Bayesian statistics: Part 1 - Bayesian vs. Frequentist
- SQLite指南(5) - PRAGMA命令用法(完整)
- 【算法学习】【图像增强】【Retinex】Retinex Image Processing(NASA)
- 信息安全技术及应用 常规加密技术
- ligerui之控件列表初始化设置
- 计算机是人类的好伴侣 作文,有电脑真好作文
- 使用stata临床决策曲线进行外部模型验证
- 联想笔记本 ThinkPad T440 Wifi无法联网的解决方法
- 单词学习-Unit8Text3-2(15年7月30日,第9天)
- RFID资产管理在学校固定资产管理中的应用,针对学校资产有效进行管理-新导智能