mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
enum CapsStates {
CAPS_OFF,
CAPS_ON
};
enum Event {
CAPS_LOCK_PRESSED,
KEY_A_PRESSED,
NO_EVENT
};
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void kasitteleTapahtuma();
void keyApressed();
void capsLockPressed();
private:
Event currentEvent;
Ui::MainWindow *ui;
CapsStates kapsLokinTila;
void appendText(QString newText);
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
kapsLokinTila = CAPS_OFF;
currentEvent=NO_EVENT;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(kasitteleTapahtuma()));
timer->start(1000);
QTimer::singleShot(3000,this,SLOT(keyApressed()));
QTimer::singleShot(4000,this,SLOT(capsLockPressed()));
QTimer::singleShot(5000,this,SLOT(keyApressed()));
QTimer::singleShot(6000,this,SLOT(capsLockPressed()));
QTimer::singleShot(7000,this,SLOT(keyApressed()));
}
void MainWindow::keyApressed(){
currentEvent=KEY_A_PRESSED;
}
void MainWindow::capsLockPressed(){
currentEvent=CAPS_LOCK_PRESSED;
}
void MainWindow::kasitteleTapahtuma(){
switch(currentEvent){
case CAPS_LOCK_PRESSED:
if(kapsLokinTila==CAPS_OFF){
kapsLokinTila=CAPS_ON;
appendText( "CAPS LOCK kytketty päälle");
}else if(kapsLokinTila==CAPS_ON){
kapsLokinTila=CAPS_OFF;
appendText( "CAPS LOCK kytketty pois");
}
currentEvent=NO_EVENT;
break;
case KEY_A_PRESSED:
if(kapsLokinTila==CAPS_OFF){
appendText( "kirjain a painettu");
}else if(kapsLokinTila==CAPS_ON){
appendText( "kirjain A painettu");
}
currentEvent=NO_EVENT;
break;
case NO_EVENT:
appendText( "*");
break;
default:
appendText( "tuntematon tapahtuma");
}
}
void MainWindow::appendText(QString newText){
QString currentText=ui->textBrowser->toPlainText();
ui->textBrowser->setText(currentText
+"\n"
+newText);
}
MainWindow::~MainWindow()
{
delete ui;
}