QT 软键盘的更加完美实现

QT 软键盘的更加完美实现

首先是软键盘的显示widget类,该类主要是用于绘制软键盘
inputwidget.h

view plaincopy to clipboardprint?
/*************************************************************************************************
*name:inputwidget.h
*describe: 用于显示软键盘的类,软键盘的显示就是该widget
*author: 颜冲<qqyanchong@163.com>
*************************************************************************************************/
#ifndef INPUTWIDGET_H
#define INPUTWIDGET_H

#include <QWidget>

class QLineEdit;
class QWSInputMethod;

namespace Ui {
class InputWidget;
}

class InputWidget : public QWidget
{
Q_OBJECT

public:
InputWidget(QWSInputMethod* im);
~InputWidget();

private:
Ui::InputWidget *ui;
};

#endif // INPUTWIDGET_H

/****************************end of this file*******************************************************/


inputwidget.cpp

view plaincopy to clipboardprint?
/*************************************************************************************************
*name:inputwidget.cpp
*describe:用于显示软键盘的类,软键盘的显示就是该widget
*author: 颜冲 <qqyanchong@163.com>
*************************************************************************************************/

#include "inputwidget.h"
#include "ui_inputwidget.h"

#include <QWSInputMethod>
#include <QSignalMapper>

/*
这里的
Qt::Tool Indicates that the widget is a tool window.
A tool window is often a small window with a smaller than usual title bar and decoration,
typically used for collections of tool buttons.
If there is a parent, the tool window will always be kept on top of it.
If there isn't a parent, you may consider using Qt::WindowStaysOnTopHint as well.
If the window system supports it, a tool window can be decorated with a somewhat lighter frame.
It can also be combined with Qt::FramelessWindowHint.

Qt::WindowStaysOnTopHint
Informs the window system that the window should stay on top of all other windows

Qt::FramelessWindowHint
Produces a borderless window. The user cannot move or resize a borderless window via the window system
*/
InputWidget::InputWidget(QWSInputMethod* im) :
QWidget(0, Qt::Tool|Qt::WindowStaysOnTopHint| Qt::FramelessWindowHint),// | Qt::FramelessWindowHint Qt::Tool
ui(new Ui::InputWidget)
{
ui->setupUi(this);


//设置信号匹配变量,设置mapper的匹配信号量的相应函数为输入法类的sendContent函数
QSignalMapper* mapper = new QSignalMapper(this);
connect(mapper, SIGNAL(mapped(const QString&)), im, SLOT(sendContent(const QString&)));

//对每一个button都匹配一个值,而且将每个按钮的点击信号量的相应函数设为mapper的map函数
mapper->setMapping(ui->btn0, "0");
connect(ui->btn0, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn1, "1");
connect(ui->btn1, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn2, "2");
connect(ui->btn2, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn3, "3");
connect(ui->btn3, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn4, "4");
connect(ui->btn4, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn5, "5");
connect(ui->btn5, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn6, "6");
connect(ui->btn6, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn7, "7");
connect(ui->btn7, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn8, "8");
connect(ui->btn8, SIGNAL(clicked()), mapper, SLOT(map()));

mapper->setMapping(ui->btn9, "9");
connect(ui->btn9, SIGNAL(clicked()), mapper, SLOT(map()));

//point
mapper->setMapping(ui->btnPoint,".");
connect(ui->btnPoint,SIGNAL(clicked()),mapper,SLOT(map()));

//Backspace
mapper->setMapping(ui->btnBackspace, "bs");
connect(ui->btnBackspace, SIGNAL(clicked()), mapper, SLOT(map()));

//ChangeSign
mapper->setMapping(ui->btnChangeSign, "cs");
connect(ui->btnChangeSign, SIGNAL(clicked()), mapper, SLOT(map()));

//clear
mapper->setMapping(ui->btnClear,"clear");
connect(ui->btnClear,SIGNAL(clicked()),mapper,SLOT(map()));

//ok
mapper->setMapping(ui->btnOk,"ok");
connect(ui->btnOk,SIGNAL(clicked()),mapper,SLOT(map()));

//close
mapper->setMapping(ui->btnClose,"close");
connect(ui->btnClose,SIGNAL(clicked()),mapper,SLOT(map()));


}

InputWidget::~InputWidget()
{
delete ui;
}

/****************************end of this file*******************************************************/


inputwidget.ui

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>InputWidget</class>
<widget class="QWidget" name="InputWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>160</width>
<height>121</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QPushButton" name="btnBackspace">
<property name="geometry">
<rect>
<x>120</x>
<y>0</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>閫€鏍?/string>
</property>
</widget>
<widget class="QPushButton" name="btn0">
<property name="geometry">
<rect>
<x>0</x>
<y>90</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
<widget class="QPushButton" name="btn1">
<property name="geometry">
<rect>
<x>0</x>
<y>60</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QPushButton" name="btn6">
<property name="geometry">
<rect>
<x>80</x>
<y>30</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>6</string>
</property>
</widget>
<widget class="QPushButton" name="btn7">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>7</string>
</property>
</widget>
<widget class="QPushButton" name="btn9">
<property name="geometry">
<rect>
<x>80</x>
<y>0</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>9</string>
</property>
</widget>
<widget class="QPushButton" name="btnChangeSign">
<property name="geometry">
<rect>
<x>80</x>
<y>90</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>卤</string>
</property>
</widget>
<widget class="QPushButton" name="btnOk">
<property name="geometry">
<rect>
<x>120</x>
<y>60</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>纭</string>
</property>
</widget>
<widget class="QPushButton" name="btnClose">
<property name="geometry">
<rect>
<x>120</x>
<y>90</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>鍏抽棴</string>
</property>
</widget>
<widget class="QPushButton" name="btn2">
<property name="geometry">
<rect>
<x>40</x>
<y>60</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
<widget class="QPushButton" name="btnClear">
<property name="geometry">
<rect>
<x>120</x>
<y>30</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>娓呴櫎</string>
</property>
</widget>
<widget class="QPushButton" name="btn5">
<property name="geometry">
<rect>
<x>40</x>
<y>30</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
<widget class="QPushButton" name="btn4">
<property name="geometry">
<rect>
<x>0</x>
<y>30</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>4</string>
</property>
</widget>
<widget class="QPushButton" name="btn3">
<property name="geometry">
<rect>
<x>80</x>
<y>60</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>3</string>
</property>
</widget>
<widget class="QPushButton" name="btnPoint">
<property name="geometry">
<rect>
<x>40</x>
<y>90</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>.</string>
</property>
</widget>
<widget class="QPushButton" name="btn8">
<property name="geometry">
<rect>
<x>40</x>
<y>0</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>8</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

imframe.h
//说明这里的软键盘将物理键盘的事件也进行了相应

view plaincopy to clipboardprint?
/*************************************************************************************************
*name:imframe.h
*describe:输入法类的实现
*author: 颜冲<qqyanchong@163.com>
*************************************************************************************************/


#ifndef IMFRAME_H
#define IMFRAME_H

#include <QWSInputMethod>
#include "inputwidget.h"
#include <QTimer>
//class InputWidget;
class IMFrame:public QWSInputMethod
{
Q_OBJECT
public:
IMFrame();
~IMFrame();
void setMicroFocus(int, int);
void updateHandler(int);

public:
void show_keyboard();
void hide_keyboard();
bool keyboard_hide();

private:
InputWidget* inputwidget;
QString content;


int fd;
QTimer *timer;

private slots:
void sendContent(const QString&);
//void confirmContent();

void phsical_kbd();
};

#endif

/*************************************************end of this file*****************************/


imframe.cpp
view plaincopy to clipboardprint?
/*************************************************************************************************
*name:imframe.h
*describe:输入法类的实现
*author: 颜冲<qqyanchong@163.com>
*************************************************************************************************/

#include <QDebug>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QInputMethodEvent>
#include "imframe.h"
#include "inputwidget.h"
#include <QWSServer>
#include <QTimer>
#include <QFile>

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define DEVICENAME "/dev/s3c2440-kb"

IMFrame::IMFrame()
{
//首先指明该输入法的软键盘的widget,并进行相应的设置
inputwidget = new InputWidget(this);
inputwidget->setGeometry(200,100,160,121);
//这里的打开设备文件是为了处理物理键盘的
fd = open(DEVICENAME, O_RDWR);
if (fd == -1)
{
printf("open device button errr!/n");
//return 0;
}
//定时器每隔500ms读一次文件,并根据读到的值进行相应的处理
timer = new QTimer( this );
connect( timer, SIGNAL(timeout()), this, SLOT(phsical_kbd()));
timer->start(500);
}

IMFrame::~IMFrame()
{
close(fd);
}


/****************************************************************
*func:sendContent
*describe:向目标窗体发送事件
*author: 颜冲 <qqyanchong@163.com>
********************************************************************/
void IMFrame::sendContent(const QString& newcontent)
{
//这里根据每个按钮的不同的值,相应的可以发送不同的按键事件
if(newcontent=="bs")
QWSServer::sendKeyEvent(0x01000003,Qt::Key_Backspace,Qt::NoModifier,true,false);
else if(newcontent=="close")
inputwidget->hide();
else if(newcontent=="ok")
inputwidget->hide();
else if(newcontent=="clear"){
QWSServer::sendKeyEvent(0x41,Qt::Key_A,Qt::ControlModifier,true,false);
QWSServer::sendKeyEvent(0x01000003,Qt::Key_Backspace,Qt::NoModifier,true,false);
QWSServer::sendKeyEvent(0x01000007,Qt::Key_Delete,Qt::NoModifier,true,false);
}
else
sendCommitString(newcontent);

}



void IMFrame::phsical_kbd(){
char key;
if (read(fd, &key, 1) > 0)
{
printf("get the key %c/n",key);

switch(key){
case 1:
QWSServer::sendKeyEvent(0x30,Qt::Key_0,Qt::NoModifier,true,false);
break;
case 2:
QWSServer::sendKeyEvent(0x31,Qt::Key_1,Qt::NoModifier,true,false);
break;
case 3:
QWSServer::sendKeyEvent(0x32,Qt::Key_2,Qt::NoModifier,true,false);
break;
case 4:
QWSServer::sendKeyEvent(0x33,Qt::Key_3,Qt::NoModifier,true,false);
break;
case 5:
QWSServer::sendKeyEvent(0x34,Qt::Key_4,Qt::NoModifier,true,false);
break;
case 6:
QWSServer::sendKeyEvent(0x35,Qt::Key_5,Qt::NoModifier,true,false);
break;
case 7:
QWSServer::sendKeyEvent(0x36,Qt::Key_6,Qt::NoModifier,true,false);
break;
case 8:
QWSServer::sendKeyEvent(0x37,Qt::Key_7,Qt::NoModifier,true,false);
break;
case 9:
QWSServer::sendKeyEvent(0x38,Qt::Key_8,Qt::NoModifier,true,false);
break;
case 10:
QWSServer::sendKeyEvent(0x39,Qt::Key_9,Qt::NoModifier,true,false);
break;
case 11://up A
QWSServer::sendKeyEvent(0x01000013,Qt::Key_Up,Qt::NoModifier,true,false);
break;
case 12://B
//QWSServer::sendKeyEvent(0x01000015,Qt::Key_Down,Qt::NoModifier,true,false);
QWSServer::sendKeyEvent(0x2e,Qt::Key_Period,Qt::NoModifier,true,false);
break;
case 13://C
//QWSServer::sendKeyEvent(0x01000012,Qt::Key_Left,Qt::NoModifier,true,false);
QWSServer::sendKeyEvent(0x01000003,Qt::Key_Backspace,Qt::NoModifier,true,false);
break;
case 14://rigtht D
QWSServer::sendKeyEvent(0x01000014,Qt::Key_Right,Qt::NoModifier,true,false);
break;
case 15://down E
//QWSServer::sendKeyEvent(0x01000005,Qt::Key_Enter,Qt::NoModifier,true,false);
QWSServer::sendKeyEvent(0x01000015,Qt::Key_Down,Qt::NoModifier,true,false);
break;
case 16://left F
//QWSServer::sendKeyEvent(0x01000007,Qt::Key_Delete,Qt::NoModifier,true,false);
QWSServer::sendKeyEvent(0x01000012,Qt::Key_Left,Qt::NoModifier,true,false);
break;
default:
QWSServer::sendKeyEvent(0x36,Qt::Key_6,Qt::NoModifier,true,false);
}

}
}

/****************************************************************
*func:updateHandler
*describe:处理从目标窗体中接受的事件
*author: 颜冲<qqyanchong@163.com>
********************************************************************/
void IMFrame::updateHandler(int type)
{
switch(type)
{
case QWSInputMethod::FocusOut://当某一widget失去焦点
//sendCommitString(content);
content.clear();
break;
default:
break;
}
}

/******************************************************************************************
*func:show_keyboard
*describe:显示软键盘
*author: 颜冲<qqyanchong@163.com>
*****************************************************************************************/
void IMFrame::show_keyboard(){
inputwidget->show();
}

/******************************************************************************************
*func:hide_keyboard
*describe:隐藏软键盘
*author: 颜冲 <qqyanchong@163.com>
*****************************************************************************************/
void IMFrame::hide_keyboard(){
inputwidget->hide();
}

/****************************************************************************************
*func:keyboard_hide
*describe:判断软键盘的状态
*author: 颜冲<qqyanchong@163.com>
***************************************************************************************/
bool IMFrame::keyboard_hide(){
return inputwidget->isHidden();
}


/***************************************end of this file***************/
分享到:
# 上一篇:Java经典面试题
查看评论
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值