key
Max Reference
Open Help

key

Report keyboard presses

Description

Tracks and outputs the key-codes, ASCII values, and modifier-key values of key presses on the computer keyboard.

Examples

Keys typed on the computer keyboard can be used to trigger messages

Arguments

None.

Attributes

Common Box Attributes

annotation [symbol]

Sets the text that will be displayed in the Clue window when the user moves the mouse over the object.

background [int] (default: 0)

Adds or removes the object from the patcher's background layer. background 1 adds the object to the background layer, background 0 removes it. Objects in the background layer are shown behind all objects in the default foreground layer.

color [4 floats]

Sets the color for the object box outline.

fontface [int]

Sets the type style used by the object. The options are:

plain
bold
italic
bold italic

Possible values:

0 = 'regular'
1 = 'bold'
2 = 'italic'
3 = 'bold italic'

fontname [symbol]

Sets the object's font.

fontsize [float]

Sets the object's font size (in points).

Possible values:

'8'
'9'
'10'
'11'
'12'
'13'
'14'
'16'
'18'
'20'
'24'
'30'
'36'
'48'
'64'
'72'

hidden [int] (default: 0)

Toggles whether an object is hidden when the patcher is locked.

hint [symbol]

Sets the text that will be displayed in as a pop-up hint when the user moves the mouse over the object in a locked patcher.

ignoreclick [int] (default: 0)

Toggles whether an object ignores mouse clicks in a locked patcher.

patching_rect [4 floats] (default: 0. 0. 100. 0.)

Sets the position and size of the object in the patcher window.

position [2 floats]

g/s(set)

Sets the object's x and y position in both patching and presentation modes (if the object belongs to its patcher's presentation), leaving its size unchanged.

presentation [int] (default: 0)

Sets whether an object belongs to the patcher's presentation.

presentation_rect [4 floats] (default: 0. 0. 0. 0.)

Sets the x and y position and width and height of the object in the patcher's presentation, leaving its patching position unchanged.

rect [4 floats]

g/s(set)

Sets the x and y position and width and height of the object in both patching and presentation modes (if the object belongs to its patcher's presentation).

size [2 floats]

g/s(set)

Sets the object's width and height in both patching and presentation modes (if the object belongs to its patcher's presentation), leaving its position unchanged.

textcolor [float]

Sets the color for the object's text in RGBA format.

textjustification [int]

Text Justification

Possible values:

0 = 'left'
1 = 'center'
2 = 'right'

varname [symbol]

Sets the patcher's scripting name, which can be used to address the object by name in pattr, scripting messages to thispatcher, and the js object.

Messages

(keyboard)

The input to key comes directly from the computer keyboard. There are no inlets.

Output

int

Output is sent each time a key is depressed on the computer keyboard. For multi-key combinations (such as option-E followed by E on a Mac US keyboard), only the last key depressed will produce output.

Out left outlet: The Unicode value of the depressed key, in UTF-16. This outlet provides the ASCII value of the pressed key (i.e., it outputs the text character rather than the code for the key that was depressed).
Out second outlet: The platform-specific key code of the typed key. The codes are specific to individual physical keyboards as well operating system. The values are derived from Apple's key-code definitions (e.g., a=0, s=1, d=2, f=3). This outlet is useful in situations where you want to identify the position of the key rather than the key which was pressed (for example, when implementing a musical keyboard using the computer keyboard). The values reported for a given key position will be fixed regardless of the language chosen, and regardless of the modifiers pressed (e.g., the value reported when the key to the right of the left shift key is pressed will be 6 whether an English or a German keyboard layout is chosen.

Out third outlet: The output value is a set of bits representing the state of various modifier keys when the key was depressed. The value can be sent through the & object to create toggles set by each modifier key. The numerical output of the right outlet is listed along with the argument to the & object that will create a toggle:
key events: Outputs 128. Toggle = & 128 (reports 0 on Windows if a mouse button is down, always reports 0 on Macintosh)
Windows Control key: Outputs 384. Toggle = & 256 (system uses this so it is not reported)
Macintosh Command key: Outputs 384. Toggle = & 256 (system uses this so it is not reported)
Shift key: Outputs 640. Toggle = & 512
Caps Lock key (on): Outputs 1152. Toggle = & 1024
Windows Alt key: Outputs 2176. Toggle = & 2048 (on Windows the system uses this so it is not reported)
Macintosh Option key: Outputs 2176. Toggle = & 2048
Windows R. Mouse Button: Outputs 4224. Toggle = & 4096
Macintosh Control key: Outputs 4224. Toggle = & 4096

Out fourth outlet: The platform-independent key code of the typed key, as described here.

See Also

Name Description
atoi Convert ASCII characters to integers
hi Human Interface (gaming) device input
itoa Convert integers to UTF-8 (Unicode) characters
keyup Report key information on release
modifiers Report modifier key presses
numkey Interpret numbers typed on the keyboard
spell Convert input to UTF-8 (Unicode) codes
sprintf Format a message of words and numbers
Max Basic Tutorial 8: Keyboard and Mouse Input Max Basic Tutorial 8: Keyboard and Mouse Input