Skip to content

Commit 66a1264

Browse files
committed
Move scriptui.environment into own page; add fuller, clearer documentation on keyboardState incl. examples
1 parent 4a8bf1c commit 66a1264

File tree

4 files changed

+109
-47
lines changed

4 files changed

+109
-47
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Welcome to The Javascript Tools Guide
5858
user-interface-tools/localization-in-scriptui-objects
5959
user-interface-tools/scriptui-object-reference
6060
user-interface-tools/scriptui-class
61+
user-interface-tools/environment
6162
user-interface-tools/common-properties
6263
user-interface-tools/window-class
6364
user-interface-tools/window-object
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
.. _environment-object:
2+
3+
Environment object
4+
==================
5+
6+
This global object is available through the :ref:`scriptui-environment` property.
7+
8+
It defines attributes of the ScriptUI environment, and only contains one property.
9+
10+
Due to this object including only a single property (that is itself an object), all of the relevant documentation will be contained in this page.
11+
12+
--------------------------------------------------------------------------------
13+
14+
.. _environment-object-properties:
15+
16+
Environment object properties
17+
-----------------------------
18+
19+
The following element properties apply specifically to Environment elements:
20+
21+
.. _environment-keyboard-state:
22+
23+
Keyboard state object
24+
---------------------
25+
26+
This JavaScript object reports the active state of the keyboard at any time; that is, the current key that is down and any modifiers that are pressed.
27+
28+
This is independent of the event-handling system, which means that at any time in your script, you can use this object to check whether specific keys (such as keyboard modifiers) are pressed, and trigger alternative actions as a result.
29+
30+
It is available through the :ref:`ScriptUI-environment` object::
31+
32+
var myKeyState = ScriptUI.environment.keyboardState;
33+
34+
The Keyboard State object contains the following properties:
35+
36+
--------------------------------------------------------------------------------
37+
38+
.. _keyboard-state-keyName:
39+
40+
keyName
41+
*******
42+
Type: ``String``
43+
44+
The name of the key currently pressed. This is the JavaScript name, a string such as ``"A"`` or ``"a"``.
45+
46+
.. note::
47+
48+
- This only works for single keys being pressed; holding multiple will report ``undefined``.
49+
- Modifier keys will report ``undefined``; to get those, see :ref:`keyboard-state-metaKeys`
50+
51+
For example, with 'a' pressed::
52+
53+
var currentPressedKey = ScriptUI.environment.keyboardState.keyName;
54+
55+
alert(currentPressedKey); // "A"
56+
57+
--------------------------------------------------------------------------------
58+
59+
.. _keyboard-state-metaKeys:
60+
61+
shiftKey, ctrlKey, altKey, metaKey
62+
**********************************
63+
Type: ``Boolean``
64+
65+
``true`` if the named modifier key is currently active.
66+
67+
.. note::
68+
69+
``metaKey`` captures both the ``META`` and ``COMMAND`` keys.
70+
71+
Examples
72+
++++++++
73+
74+
For example, checking whether a modifier key is held during script execution::
75+
76+
var shiftHeld = ScriptUI.environment.keyboardState.shiftKey;
77+
78+
if (shiftHeld) {
79+
alert("User is holding shift!");
80+
}
81+
82+
Or to check for keyboard modifier combinations::
83+
84+
var keyboardState = ScriptUI.environment.keyboardState;
85+
86+
if (keyboardState.shiftKey && keyboardState.altKey) {
87+
alert("Shift and alt held!");
88+
}
89+
90+
This can also be used within interface buttons as alternative to :ref:`checking the modifiers via keyboard events <keyboardevent-object-getModifierState>`, which can be more confusing and less user-intuitive, unless you're confident you're handling event states properly.
91+
92+
For example::
93+
94+
button.onClick = function () {
95+
if (ScriptUI.environment.keyboardState.shiftKey) {
96+
// Special functionality for 'shift' key here
97+
return;
98+
}
99+
100+
// normal button behaviour here
101+
}

docs/user-interface-tools/event-handling.rst

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Several helper classes provide low-level event-handling capabilities.
77
- Event objects are normally created by ScriptUI and passed to your event handler. However, you can
88
simulate a user action by constructing an event object using :ref:`ScriptUI-events-createEvent`,
99
and sending it to a target object's `controlobj-dispatchEvent` function.
10-
- A helper object, :ref:`Keyboard-state-object`, provides global access to the keyboard state during function
10+
- A helper object, :ref:`environment-keyboard-state`, provides global access to the keyboard state during function
1111
execution, outside the event-handling framework.
1212

1313
--------------------------------------------------------------------------------
@@ -300,6 +300,10 @@ getModifierState()
300300

301301
Returns true if the given modifier was active when the event occurred, false otherwise.
302302

303+
.. note::
304+
305+
If you're trying to check whether keyboard modifier keys (alt/ctrl/meta/shift) are held down at any time in your script, not just in an event, see :ref:`environment-keyboard-state`.
306+
303307
--------------------------------------------------------------------------------
304308

305309
.. _keyboardevent-object-initKeyboardEvent:
@@ -528,33 +532,3 @@ Reinitializes the object, allowing you to change the event properties after cons
528532
set the corresponding properties.
529533

530534
Returns ``undefined``.
531-
532-
--------------------------------------------------------------------------------
533-
534-
.. _keyboard-state-object:
535-
536-
Keyboard state object
537-
---------------------
538-
This JavaScript object reports the active state of the keyboard at any time; that is, the current key that is
539-
down and any modifiers that are pressed. It is independent of the event-handling system, and is available
540-
through the :ref:`ScriptUI-environment` object::
541-
542-
myKeyState = ScriptUI.environment.keyboardState;
543-
544-
The object has the following properties:
545-
546-
keyName
547-
*******
548-
Type: ``String``
549-
550-
The name of the key currently pressed. This is the JavaScript name, a
551-
string such as ``"A"`` or ``"a"``.
552-
553-
--------------------------------------------------------------------------------
554-
555-
shiftKey, ctrlKey, altKey, metaKey
556-
**********************************
557-
Type: ``Boolean``
558-
559-
True if the named modifier key is currently active.
560-

docs/user-interface-tools/scriptui-class.rst

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ environment; contains a Keyboard state object that reports the active
8585
state of the keyboard at any time, independent of the event-handling
8686
framework.
8787

88+
See: :ref:`environment-object` for more information.
89+
8890
--------------------------------------------------------------------------------
8991

9092
.. _scriptui-events:
@@ -226,19 +228,3 @@ Creates a new image object for use in controls that can display images, loading
226228
images from the specified resources or image files.
227229

228230
Returns a :ref:`ScriptUIImage-object`.
229-
230-
--------------------------------------------------------------------------------
231-
232-
.. _environment-object:
233-
234-
Environment object
235-
------------------
236-
This global object is available through the :ref:`ScriptUI.environment <scriptui-environment>` property. It defines attributes of the
237-
ScriptUI environment. In the current release, it contains one property:
238-
239-
keyboardState
240-
*************
241-
``Object``
242-
243-
A :ref:`Keyboard-state-object` that reports the active state of the keyboard at
244-
any time, independent of the event-handling framework.

0 commit comments

Comments
 (0)