参考 keyTyped()

keyTyped()

当按下具有可打印字符的键时调用一次的函数。

声明函数 keyTyped() 将设置一个代码块,当用户按下任何具有可打印字符的键时自动运行一次,如 a 或 1。修饰键如 SHIFTCONTROL 和箭头键将被忽略:

function keyTyped() { // Code to run. }

keyTyped() 由 p5.js 调用时,变量 keykeyCode 将更新为最近释放的值:

function keyTyped() { // Check for the "c" character using key. if (key === 'c') { // Code to run. } <p> // Check for "c" using keyCode. if (keyCode === 67) { // Code to run. } } </p>

参数 event 是可选的。keyTyped() 总是传递一个带有描述按键事件属性的 KeyboardEvent 对象:

function keyReleased(event) { // Code to run that uses the event. console.log(event); }

注意:使用 keyPressed() 函数和 keyCode 系统变量来响应 ALT 等修饰键。

浏览器可能会将默认行为附加到各种键事件上。为了防止此事件的任何默认行为,在函数的结尾添加 return false;

示例

语法

keyTyped([event])

参数

event
KeyboardEvent:

optional KeyboardEvent callback argument.

Notice any errors or typos? Please let us know. Please feel free to edit src/events/keyboard.js and open a pull request!

相关参考