Open this page at https://devegied.github.io/pxt-i2c-keypad/
makecode I2C Keypad extension for micro:bit
Matrix keypad with custom controller connected to micro:bit through I2C.
Code is mostly duplicated from MakerBit Touch extension
In your micro:bit makecode project click on Extensions under the gearwheel menu search for devegied/pxt-i2c-keypad and import
i2cKeypad=github:devegied/pxt-i2c-keypad
i2cKeypad.initKeypadController("123A456B789C*0#D")
Optionaly initialize the keypad controller by defining key symbols
- symbols on keypad keys from left to right and from top to bottom expressed as a string, eg: "123A456B789C*0#D"
i2cKeypad.onKey(i2cKeypad.Keys.K1, i2cKeypad.KeyAction.Pressed, () => {})
Do something when a key is pressed or released.
- key (has a picker UI), the key to be checked, eg: Keys.K1
- action (has a picker UI), the trigger action, eg: KeyAction.Pressed
- handler, code to run when the event is raised
i2cKeypad.onKeysPressed("*#", (theKey,stringBeforeTheKey) => {})
Do something when any of the symbols are entered.
- keys, any of the symbols in this string will initiate execution, eg: "*#"
- cb, code to run when the event is raised
- theKey, symbol of the key that initiated this execution
- stringBeforeTheKey, the symbols entered since the last execution as a string
i2cKeypad.currentKey()
Returns the key index of the last key event that was received. It could be either a key pressed or released event.
i2cKeypad.currentSymbol()
Returns the key symbol of the last key event that was received. It could be either a key pressed or released event.
i2cKeypad.isPressed(1)
Returns true if a specific key is currently pressed. False otherwise.
- keyIndex (has a picker UI), the key index to be checked
i2cKeypad.wasPressed()
Returns true if any key was pressed since the last call of this function. False otherwise.
First example shows key index and key symbol when keypad buttons are pressed
i2cKeypad.onKey(i2cKeypad.Keys.Any, i2cKeypad.KeyAction.Pressed, function () {
basic.showString("" + convertToText(i2cKeypad.currentKey()) + "-" + i2cKeypad.currentSymbol())
})
i2cKeypad.onKey(i2cKeypad.Keys.Any, i2cKeypad.KeyAction.Released, function () {
basic.clearScreen()
})
i2cKeypad.initKeypadController("123A456B789C*0#D")
Second example shows PINcode validation. PIN code entrance is finished by pressing #
, half entered code can be cleared by pressing *
i2cKeypad.onKeysPressed("*#", function (theKey, stringBeforeTheKey) {
if (theKey == "#" && stringBeforeTheKey == pinCode) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
}
})
let pinCode = ""
pinCode = "1234"
This image shows the blocks code from the last commit in master. This image may take a few minutes to refresh.
i2cKeypad.initKeypadController("123A456B789C*0#D")
i2cKeypad.onKey(i2cKeypad.Keys.K1, i2cKeypad.KeyAction.Pressed, () => {
})
i2cKeypad.onKeysPressed("*#", (theKey,stringBeforeTheKey) => {
})
i2cKeypad.currentKey()
i2cKeypad.currentSymbol()
i2cKeypad.isPressed(1)
wasPressed()
Licensed under the MIT License (MIT). See LICENSE file for more details.
Copyright (c) 2022, devegied Copyright (c) 2020, MakerBit
- for PXT/microbit