keyboard support for Qt GUI

This commit is contained in:
Jeff Wang 2019-11-14 03:32:07 -05:00
parent f70510e46a
commit 9402bd512f
2 changed files with 56 additions and 7 deletions

View File

@ -7,7 +7,7 @@ ApplicationWindow
width: 4 * (100 + 5)
height: 5 * (100 + 5) + 200 + 30
title: qsTr("RPN Calculator")
//calculator
Column {
id: base;
@ -25,8 +25,54 @@ ApplicationWindow
onClicked: Qt.quit()
anchors.fill: parent
}
//handle key presses from physical keyboard
focus: true //handle keypresses here
//numbers
Keys.onDigit0Pressed: _calculator.buttonClicked("4,0")
Keys.onDigit1Pressed: _calculator.buttonClicked("3,0")
Keys.onDigit2Pressed: _calculator.buttonClicked("3,1")
Keys.onDigit3Pressed: _calculator.buttonClicked("3,2")
Keys.onDigit4Pressed: _calculator.buttonClicked("2,0")
Keys.onDigit5Pressed: _calculator.buttonClicked("2,1")
Keys.onDigit6Pressed: _calculator.buttonClicked("2,2")
Keys.onDigit7Pressed: _calculator.buttonClicked("1,0")
Keys.onDigit8Pressed: _calculator.buttonClicked("1,1")
Keys.onDigit9Pressed: _calculator.buttonClicked("1,2")
//swap
Keys.onRightPressed: _calculator.buttonClicked("0,1")
//enter
Keys.onEnterPressed: _calculator.buttonClicked("4,2")
Keys.onReturnPressed: _calculator.buttonClicked("4,2")
Keys.onSpacePressed: _calculator.buttonClicked("4,2")
Keys.onPressed: {
if ((event.key == Qt.Key_Q) && (event.modifiers == Qt.ControlModifier))
Qt.quit()
else if ((event.key == Qt.Key_S))
_calculator.buttonClicked("0,0")
else if ((event.key == Qt.Key_Backspace))
_calculator.buttonClicked("0,3")
else if ((event.key == Qt.Key_Slash))
_calculator.buttonClicked("1,3")
else if ((event.key == Qt.Key_division))
_calculator.buttonClicked("1,3")
else if ((event.key == Qt.Key_Asterisk))
_calculator.buttonClicked("2,3")
else if ((event.key == Qt.Key_Minus))
_calculator.buttonClicked("3,3")
else if ((event.key == Qt.Key_Plus))
_calculator.buttonClicked("4,3")
else if ((event.key == Qt.Key_N)) //negate
_calculator.buttonClicked("0,2")
else if ((event.key == Qt.Key_Equal)) //enter
_calculator.buttonClicked("4,2")
else if ((event.key == Qt.Key_Period)) // "."
_calculator.buttonClicked("4,1")
else if ((event.key == Qt.Key_E)) // also "."
_calculator.buttonClicked("4,1")
}
}
//LCD
Rectangle {
objectName: "lcd";
@ -40,7 +86,7 @@ ApplicationWindow
anchors.centerIn: parent
}
}
//Keyboard
Repeater {
model: 5;
@ -74,6 +120,7 @@ ApplicationWindow
anchors.centerIn: parent
}
MouseArea {
//get row/column
onClicked: _calculator.buttonClicked(parent.parent.objectName + "," + index)
anchors.fill: parent
}
@ -94,7 +141,7 @@ ApplicationWindow
return "<b>" + keys[row][col] + "</b>"
}
function getShiftedText(row, col) {
var shifted_keys = [
["Shift", "1/x", " √<span style=\"text-decoration: overline\">x</span> ", "CL<i>x</i>"],
@ -103,10 +150,10 @@ ApplicationWindow
["", "", "", ""],
["off", "STO", "RCL", "LAST<i>x</i>"]
]
return "<small>" + shifted_keys[row][col] + "</small>"
}
function getBackgroundColor(row, col) {
var background_color = [
["white", "white", "white", "#ff9494"],
@ -115,7 +162,7 @@ ApplicationWindow
["#eeeeee", "#eeeeee", "#eeeeee", "white"],
["#eeeeee", "#eeeeee", "white", "white"]
]
return background_color[row][col]
}
}

View File

@ -88,6 +88,8 @@ static int is_valid_character(char letter){
return 1;
} else if(letter == '^'){
return 1;
} else if(letter == 'E' || letter == 'r' || letter == 'o'){
return 1;
}
return 0;