Keyboard NOKIA

About

Theย NOKIA Keyboard embraces the iconic layout synonymous with NOKIA phones to enter letters (and numbers). For instance, to type the letter โ€œE,โ€ press the third button twice. The illustration below shows how much times you have to push a button quickly to get a letter.

The code:

let separatorAfter, separatorTimeout, separatorLength = 1;
let submitAfter, submitTimeout;

let decimalMarker = 0; // language based
let lastCharLength = 1;
let lastAutoSeparator = false;

let actualValue = โ€˜โ€™;

function main() {
let separator = config.get(โ€™keyboard.separator.keyโ€™);
if (strCharAt(separator, 1) !== โ€˜#โ€™) separatorLength = strLen(separator);

separatorAfter = config.get(โ€™keyboard.separator.afterโ€™);
submitAfter = config.get(โ€™keyboard.submit.afterโ€™)

ps.connect();
resetValue();
}

function resetValue() {
actualValue = โ€˜โ€™;
}

function peekValue() {
ps.print(actualValue);
}

function sendSeparator(charLength) {
keyboard.send(โ€™#SEPARATORโ€™);
resetValue();
lastCharLength = separatorLength + charLength;
actualValue = โ€˜โ€™;
}

function sendSubmit() {
if (lastAutoSeparator) {
for (let i = 0; i < separatorLength; i++) {
keyboard.send(โ€™#BSโ€™);
}
}
keyboard.send(โ€™#SUBMITโ€™);
lastCharLength = 1;
actualValue = โ€˜โ€™;
atom.vibrate(โ€™.โ€™);
}

let mapping = [
, , ,
, , ,
, , ,
, ,
];

function handleAfter() {
peekValue();

lastAutoSeparator = false;
if (separatorAfter > 0) {
if (separatorAfter < 99) { // after x character
if (strLen(actualValue) >= separatorAfter) {
lastAutoSeparator = true;
sendSeparator(lastCharLength);
}
} else {
clearTimeout(separatorTimeout);
separatorTimeout = setTimeout(function() {
lastAutoSeparator = true;
sendSeparator(lastCharLength);
}, separatorAfter);
}
}
if (submitAfter > 0) {
if (separatorAfter < 99) { // after x character
clearTimeout(submitTimeout);
}
}
}

function handleKey(key) {
if (keyboard.isSubmit(key)) {
sendSubmit();
return;
}

if (keyboard.isCharacter(key)) {
keyboard.send(key);
lastCharLength = strLen(key);
actualValue += key;
handleAfter();
return;
}

if (keyboard.isBackspace(key)) {
for (let i = 0; i < lastCharLength; i++) {
keyboard.send(key);
actualValue = strSub(actualValue, 0, -1);
}
lastCharLength = 1;
handleAfter();
return;
}

if (keyboard.isSeparator(key)) {
sendSeparator(0);
handleAfter();
}
}

function onAtomButtonClick(keyCode, count) {
let key = mapping;
if (typeof key === โ€˜stringโ€™ && strCharAt(key, 0) !== โ€˜#โ€™) {
if (strLen(key) >= count + 1) {
key = strCharAt(key, count);
} else {
return;
}
}
handleKey(key);
}

function onAtomButtonLongPress(keyCode) {
let key = mapping;
handleKey(key);
}

function onAtomButtonRepeatPress(keyCode) {
let key = mapping;
handleKey(key);
}

function onEvent(e) {
if (e.source === โ€˜atom:buttonโ€™) {
const buttonId = parseInt(e.value);
if (e.type === โ€˜clickโ€™) {
return onAtomButtonClick(buttonId, 0);
}
if (e.type === โ€˜longpressโ€™) {
return onAtomButtonLongPress(buttonId);
}
if (e.type === โ€˜repeatpressโ€™) {
return onAtomButtonRepeatPress(buttonId);
}
if (strSub(e.type, 0, 5) === โ€˜clickโ€™ && strLen(e.type) === 6) {
let clickCount = parseInt(strCharAt(e.type, 5));
onAtomButtonClick(buttonId, clickCount - 1);
}
}
}