Input
Input
Class to handle user input, and trigger appropriate event listeners.
Constructor
public Input
new Input(canvas, dragTime)
Initializes Input with given canvas and dragTime.
Parameters
canvas: HTMLCanvasElement
– The canvas input is being applied to.dragTime: number
– The minimum length of time a mousedown must last to be considered a drag rather than a click.
Properties
No publicly accessible properties on Input
private canvas: HTMLCanvasElement
The canvas the user is performing inputs on.
private prevMousePos: Vector
A vector representing the previous position of the mouse.
private mousePos: Vector
A vector representing the current position of the mouse.
private mouseDown: false | true
True if a mousebutton is held down, false otherwise.
private mouseDownPos: Vector
A vector representing the position the mouse was when the mousebutton first became pressed.
private mouseDownButton: number
Represents the mousebutton being pressed (left, middle, right, etc.).
private isDragging: false | true
True if the mouse is being dragged, false otherwise. (a "drag" being distinct from a "click").
private startTapTime: number
Represents the time at which the mouse button became held down.
private touchCount: number
Represents the number of touches currently active (i.e. fingers on a touchpad or mobile device).
private keysDown: Map<"Alt" | "AltGraph" | "CapsLock" | "Control" | "Fn" | "FnLock" | "Hyper" | "Meta" | "NumLock" | "ScrollLock" | "Shift" | "Super" | "Symbol" | "SymbolLock" | "Enter" | "Tab" | " " | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "ArrowUp" | "End" | "Home" | "PageDown" | "PageUp" | "Backspace" | "Clear" | "Copy" | "CrSel" | "Cut" | "Delete" | "EraseEof" | "ExSel" | "Insert" | "Paste" | "Redo" | "Undo" | "Accept" | "Again" | "Attn" | "Cancel" | "ContextMenu" | "Escape" | "Execute" | "Find" | "Finish" | "Help" | "Pause" | "Play" | "Props" | "Select" | "ZoomIn" | "ZoomOut" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "F13" | "F14" | "F15" | "F16" | "F17" | "F18" | "F19" | "F20" | "Soft1" | "Soft2" | "Soft3" | "Soft4" | "Decimal" | "Key11" | "Key12" | "Multiply" | "Add" | "Divide" | "Subtract" | "Separator" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z", false | true>
Map with keycodes as keys and booleans representing whether that key is held as values.
private dragTime: number
Amount of time a mousebutton needs to be held down to be considered a "drag" (rather than a "click").
private blocked: false | true
If true, "blocks" Input, stopping listeners from triggering events.
Methods
private isPreventedCombination
isPreventedCombination(newKey) => false | true
Checks if newKey is a prevented combination of keys.
Parameters
-
newKey: "Alt" | "AltGraph" | "CapsLock" | "Control" | "Fn" | "FnLock" | "Hyper" | "Meta" | "NumLock" | "ScrollLock" | "Shift" | "Super" | "Symbol" | "SymbolLock" | "Enter" | "Tab" | " " | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "ArrowUp" | "End" | "Home" | "PageDown" | "PageUp" | "Backspace" | "Clear" | "Copy" | "CrSel" | "Cut" | "Delete" | "EraseEof" | "ExSel" | "Insert" | "Paste" | "Redo" | "Undo" | "Accept" | "Again" | "Attn" | "Cancel" | "ContextMenu" | "Escape" | "Execute" | "Find" | "Finish" | "Help" | "Pause" | "Play" | "Props" | "Select" | "ZoomIn" | "ZoomOut" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "F13" | "F14" | "F15" | "F16" | "F17" | "F18" | "F19" | "F20" | "Soft1" | "Soft2" | "Soft3" | "Soft4" | "Decimal" | "Key11" | "Key12" | "Multiply" | "Add" | "Divide" | "Subtract" | "Separator" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
– Represents the key combination being pressed.
Returns
-
false | true
– True if newKey is a prevented combination, false otherwise.
private hookupKeyboardEvents
hookupKeyboardEvents() => void
Sets up Listeners for all keyboard Events.
public reset
reset() => void
Resets internal state of the Input. Keeps listeners and outer-controlled state.
public addListener
addListener(listener) => void
Adds a Listener to the list of Listeners Events are checked against.
Parameters
-
listener: Listener
– The Listener being added.
public removeListener
removeListener(listener) => void
Removes a Listener from the list of Listeners Events are checked against.
Parameters
-
listener: Listener
– The Listener being removed.
public isMouseDown
isMouseDown() => false | true
Checks if the mouse is pressed down.
Returns
-
false | true
– True if the mouse is down, false otherwise.
public isKeyDown
isKeyDown(key) => false | true
Checks if the given key is held down.
Parameters
-
key: "Alt" | "AltGraph" | "CapsLock" | "Control" | "Fn" | "FnLock" | "Hyper" | "Meta" | "NumLock" | "ScrollLock" | "Shift" | "Super" | "Symbol" | "SymbolLock" | "Enter" | "Tab" | " " | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "ArrowUp" | "End" | "Home" | "PageDown" | "PageUp" | "Backspace" | "Clear" | "Copy" | "CrSel" | "Cut" | "Delete" | "EraseEof" | "ExSel" | "Insert" | "Paste" | "Redo" | "Undo" | "Accept" | "Again" | "Attn" | "Cancel" | "ContextMenu" | "Escape" | "Execute" | "Find" | "Finish" | "Help" | "Pause" | "Play" | "Props" | "Select" | "ZoomIn" | "ZoomOut" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "F13" | "F14" | "F15" | "F16" | "F17" | "F18" | "F19" | "F20" | "Soft1" | "Soft2" | "Soft3" | "Soft4" | "Decimal" | "Key11" | "Key12" | "Multiply" | "Add" | "Divide" | "Subtract" | "Separator" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
– Represents the key being checked.
Returns
-
false | true
– True if key is down, false otherwise.
public isShiftKeyDown
isShiftKeyDown() => false | true
Checks if the shift key is held down.
Returns
-
false | true
– True if the shift key is down, false otherwise.
public isEscKeyDown
isEscKeyDown() => false | true
Checks if the option key is held down.
Returns
-
false | true
– True if the option key is down, false otherwise.
public isModifierKeyDown
isModifierKeyDown() => false | true
Checks if the modifier key is held down.
Returns
-
false | true
– True if the modifier key (control, command, or meta) is down, false otherwise.
public isAltKeyDown
isAltKeyDown() => false | true
Checks if the option key is held down.
Returns
-
false | true
– True if the option key is down, false otherwise.
public getMousePos
getMousePos() => Vector
Gets the position of the cursor of the mouse.
Returns
-
Vector
– Current position of the mouse.
public getMouseDownPos
getMouseDownPos() => Vector
Gets the position where the mouse was pressed down.
Returns
-
Vector
– Current position of the mouse down.
public getDeltaMousePos
getDeltaMousePos() => Vector
Gets the difference between the current and previous mouse position.
Returns
-
Vector
– Difference between current and previous mouse position.
public getTouchCount
getTouchCount() => number
Gets the number of times the mouse has been pressed.
Returns
-
number
– The touchCount.
protected onKeyDown
onKeyDown(key) => void
Sets the given key as down, and calls each Listener on Event "keydown", key.
Parameters
-
key: "Alt" | "AltGraph" | "CapsLock" | "Control" | "Fn" | "FnLock" | "Hyper" | "Meta" | "NumLock" | "ScrollLock" | "Shift" | "Super" | "Symbol" | "SymbolLock" | "Enter" | "Tab" | " " | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "ArrowUp" | "End" | "Home" | "PageDown" | "PageUp" | "Backspace" | "Clear" | "Copy" | "CrSel" | "Cut" | "Delete" | "EraseEof" | "ExSel" | "Insert" | "Paste" | "Redo" | "Undo" | "Accept" | "Again" | "Attn" | "Cancel" | "ContextMenu" | "Escape" | "Execute" | "Find" | "Finish" | "Help" | "Pause" | "Play" | "Props" | "Select" | "ZoomIn" | "ZoomOut" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "F13" | "F14" | "F15" | "F16" | "F17" | "F18" | "F19" | "F20" | "Soft1" | "Soft2" | "Soft3" | "Soft4" | "Decimal" | "Key11" | "Key12" | "Multiply" | "Add" | "Divide" | "Subtract" | "Separator" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
– Represents the key being pressed.
protected onKeyUp
onKeyUp(key) => void
Sets the given key as up, and calls each Listener on Event "keyup", key.
Parameters
-
key: "Alt" | "AltGraph" | "CapsLock" | "Control" | "Fn" | "FnLock" | "Hyper" | "Meta" | "NumLock" | "ScrollLock" | "Shift" | "Super" | "Symbol" | "SymbolLock" | "Enter" | "Tab" | " " | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "ArrowUp" | "End" | "Home" | "PageDown" | "PageUp" | "Backspace" | "Clear" | "Copy" | "CrSel" | "Cut" | "Delete" | "EraseEof" | "ExSel" | "Insert" | "Paste" | "Redo" | "Undo" | "Accept" | "Again" | "Attn" | "Cancel" | "ContextMenu" | "Escape" | "Execute" | "Find" | "Finish" | "Help" | "Pause" | "Play" | "Props" | "Select" | "ZoomIn" | "ZoomOut" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "F13" | "F14" | "F15" | "F16" | "F17" | "F18" | "F19" | "F20" | "Soft1" | "Soft2" | "Soft3" | "Soft4" | "Decimal" | "Key11" | "Key12" | "Multiply" | "Add" | "Divide" | "Subtract" | "Separator" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
– Represents the key being released.
protected onClick
onClick(_, button) => void
Calls each Listener on Event "click", button.
Parameters
_: Vector
– Unused position vector.button: number
– Represents the mouse button being clicked (left mouse button by default).
protected onDoubleClick
onDoubleClick(button) => void
Calls each Listener on Event "dbclick", button.
Parameters
-
button: number
– Represents the mouse button being double clicked.
protected onScroll
onScroll(delta) => void
Calls each Listener on Event "zoom", zoomFactor, mousePos where zoomFactor is calculated from delta.
Parameters
-
delta: number
– Represents whether the user is zooming in or out (negative and positive, respectively).
protected onMouseDown
onMouseDown(pos, button) => void
Adjusts mouse variables (dragging, position, etc.), and triggers Listeners on Event "mousedown", button.
Parameters
pos: Vector
– Represents the position of the mouse being pressed.button: number
– Represents the mouse button being pressed (0 by default).
protected onMouseMove
onMouseMove(pos) => void
Triggered on mouse movement, calculates new mouse position, and triggers Listeners on Event "mousemove", as well as Listeners on Event "mousedrag", [current mouse button down] if the user is clicking.
Parameters
-
pos: Vector
– Represents the new absolute position of the mouse.
protected onMouseUp
onMouseUp(button) => void
Calls each Listener on Event "mouseup", button and adjusts variables tracking mouse buttons.
Parameters
-
button: number
– Represents the mouse button being released (0 by default).
protected onMouseLeave
onMouseLeave() => void
Calls each Listener on Event "mouseleave". Also calls on Event "mouseup", [current mouse button down].
protected onTouchStart
onTouchStart(touches) => void
Calls onMouseDown for the midpoint of multiple touches.
Parameters
-
touches: Vector[]
– Represents the positions of the touches.
protected onTouchMove
onTouchMove(touches) => void
Called when a user moves a touch point (as when using a touchpad or mobile device). Calls onMouseMove for the midpoint of multiple movements.
Parameters
-
touches: Vector[]
– Represents the positions of the touches.
private callListeners
callListeners(event) => void
Calls the Listeners in 'listeners' for Event 'event', if Input not blocked.
Parameters
-
event: MouseEvent | KeyboardEvent | ZoomEvent | CopyPasteEvent | OtherEvent
– Event being given to the Listeners.
Static Methods
No static methods for Input