首页 > 文档 > keyPressed()键盘按键函数
2017
07-14

keyPressed()键盘按键函数

2-3.keyPressed()

Name(名称):keyPressed()//键盘按键中断函数

Examples(例子):

// Click on the image to give it focus, 单击窗口使其成为焦点

// and then press any key.
然后按任意键

int value = 0;

void draw() {

fill(value);

rect(25, 25, 50, 50);

}

void keyPressed() {

if (value == 0) {

value = 255;

} else {

value = 0;

}

}

Description(描述):The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable.

keyPressed()函数每次按下一次时都会被调用一次。按下的键被存储在键变量中。

For non-ASCII keys, use the keyCode variable. The keys included in the ASCII specification (BACKSPACE, TAB, ENTER, RETURN, ESC, and DELETE) do not require checking to see if the key is coded; for those keys, you should simply use the key variable directly (and not keyCode). If you’re making cross-platform projects, note that the ENTER key is commonly used on PCs and Unix, while the RETURN key is used on Macs. Make sure your program will work on all platforms by checking for both ENTER and RETURN.

对于非ASCII键,请使用keyCode变量。 ASCII规范(BACKSPACE,TAB,ENTER,RETURN,ESC和DELETE)中包含的密钥不需要检查密钥是否被编码;对于那些键,你应该直接使用键变量(而不是keyCode)。如果您正在开发跨平台项目,请注意,ENTER键通常用于PC和Unix,而RETURN键则在Mac上使用。通过检查ENTER和RETURN,确保您的程序可以在所有平台上运行。

Because of how operating systems handle key repeats, holding down a key may cause multiple calls to keyPressed(). The rate of repeat is set by the operating system, and may be configured differently on each computer.

由于操作系统如何处理密钥重复,按住一个键可能会导致对keyPressed()的多次调用。重复率由操作系统设置,并且可以在每台计算机上进行不同的配置。

Note that there is a similarly named boolean variable called keyPressed. See its reference page for more information.

请注意,有一个名为keyPressed的类似名称的布尔变量。有关详细信息,请参阅其参考页面。

Mouse and keyboard events only work when a program has draw(). Without draw(), the code is only run once and then stops listening for events.

鼠标和键盘事件只有在程序具有draw()时才可以工作。没有draw(),代码只运行一次,然后停止侦听事件。

Syntax(语法):keyPressed()

keyPressed(event)

Returns(返回值):Void(

Related(相关函数):key
keyCode
keyPressed
keyReleased()



最后编辑:
作者:卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。