您的位置:首页 > 编程语言 > Qt开发

c进程读取按键输入,通过socket传输给qt,qt按键线程接收到后,提交到Qt的事件处理系统中

2015-05-07 17:16 686 查看


#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

#include <QDebug>

#include <QCoreApplication>

#include <QMouseEvent>

#include <QTimer>

namespace Ui {

class Widget;

}

class Widget : public QWidget

{

Q_OBJECT

public:

explicit Widget(QWidget *parent = 0);

~Widget();

protected:

private slots:

void ZSlotMove();

void ZSlotOkay();

void ZSlotBtn1();

void ZSlotBtn2();

void ZSlotBtn3();

void ZSlotBtn4();

void ZSlotTimeout();

private:

Ui::Widget *ui;

QTimer *m_timer;

};

#endif // WIDGET_H

#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::Widget)

{

ui->setupUi(this);

ui->btn1->setFocusPolicy(Qt::StrongFocus);

ui->btn2->setFocusPolicy(Qt::StrongFocus);

ui->btn3->setFocusPolicy(Qt::StrongFocus);

ui->btn4->setFocusPolicy(Qt::StrongFocus);

ui->btnMove->setFocusPolicy(Qt::StrongFocus);

ui->btnOkay->setFocusPolicy(Qt::StrongFocus);

connect(ui->btn1,SIGNAL(clicked()),this,SLOT(ZSlotBtn1()));

connect(ui->btn2,SIGNAL(clicked()),this,SLOT(ZSlotBtn2()));

connect(ui->btn3,SIGNAL(clicked()),this,SLOT(ZSlotBtn3()));

connect(ui->btn4,SIGNAL(clicked()),this,SLOT(ZSlotBtn4()));

connect(ui->btnMove,SIGNAL(clicked()),this,SLOT(ZSlotMove()));

connect(ui->btnOkay,SIGNAL(clicked()),this,SLOT(ZSlotOkay()));

ui->btn1->setFocus();

this->m_timer=new QTimer;

connect(this->m_timer,SIGNAL(timeout()),this,SLOT(ZSlotTimeout()));

this->m_timer->start(1000);

}

Widget::~Widget()

{

delete ui;

}

void Widget::ZSlotTimeout()

{

QWidget *tCurFocsWidget=QApplication::focusWidget();

if(tCurFocsWidget)

{

//post a tab key to change the current widget.

QKeyEvent *tTabKeyPress=new QKeyEvent( QEvent::KeyPress,Qt::Key_Tab,Qt::NoModifier);

QApplication::postEvent(tCurFocsWidget,tTabKeyPress);

//generate a mouse press and mouse release event.

QMouseEvent *tMousePress=new QMouseEvent(QEvent::MouseButtonPress,QPoint(2,2),Qt::LeftButton,Qt::NoButton,Qt::NoModifier);

QApplication::postEvent(tCurFocsWidget->focusWidget(),tMousePress);

QMouseEvent *tMouseRelease=new QMouseEvent(QEvent::MouseButtonRelease,QPoint(2,2),Qt::LeftButton,Qt::NoButton,Qt::NoModifier);

QApplication::postEvent(tCurFocsWidget->focusWidget(),tMouseRelease);

}else{

qDebug()<<"no global focus widget";

}

}

void Widget::ZSlotMove()

{

qDebug()<<"move button was clicked:";

}

void Widget::ZSlotOkay()

{

qDebug()<<"okay button was clicked:";

}

void Widget::ZSlotBtn1()

{

qDebug()<<"button1 was clicked:";

}

void Widget::ZSlotBtn2()

{

qDebug()<<"button2 was clicked:";

}

void Widget::ZSlotBtn3()

{

qDebug()<<"button3 was clicked:";

}

void Widget::ZSlotBtn4()

{

qDebug()<<"button4 was clicked:";

}

刚试验出来,奶奶的不容易呀。

设备上有几个按键,写一个c程序用于不断的读取按键的,泥玛,没有接到中断上,只好每隔100ms读一次,真服了画板的人。

上下左右,用于移动光标,

这里暂时只实现了模拟Tab按键,一直向下,应该加上向左向右,等功能。

确定按键就是postEvent一个鼠标左键的Press和Release一对值就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐