Class Keyboard
- Namespace
- Ranorex
- Assembly
- Ranorex.Core.dll
Provides keyboard actions and events.
public class Keyboard
- Inheritance
-
Keyboard
- Inherited Members
Constructors
Keyboard()
public Keyboard()
Fields
DefaultScanCode
Specify this value if you want to use the default hardware scan code corresponding to a specified virtual key code (see Keys).
public const int DefaultScanCode = 0
Field Value
Properties
AbortKey
Gets or sets a key-modifier combination that aborts the current thread if pressed.
public static Keys AbortKey { get; set; }
Property Value
Remarks
If the abort key is pressed, the thread that set the AbortKey is aborted; i.e. a ThreadAbortException is raised in the thread that set the AbortKey.
CharacterMappingEnabled
Gets or sets a value whether Unicode characters are tried to be mapped to virtual keyboard inputs.
public static bool CharacterMappingEnabled { get; set; }
Property Value
- bool
Trueto enable Unicode character mapping,falseto disable. Default istrue.
Remarks
If this property is true, in the keyboard methods that take Unicode characters as input
the Unicode characters are tried to be mapped to virtual keyboard inputs instead of being
directly send to the system as Unicode character input.
If you are working with multiple input locales at the same time, it is usually better to
turn character mapping off, i.e. to set this property to false.
DefaultKeyPressTime
Gets or sets the default time for a key press, i.e. the time elapsed between a key being pressed (KeyDown event) and released (KeyUp event) again.
public static Duration DefaultKeyPressTime { get; set; }
Property Value
Exceptions
- ArgumentOutOfRangeException
If the value is negative.
Enabled
Gets or sets a value indicating whether physical keyboard actions are enabled.
public static bool Enabled { get; set; }
Property Value
Remarks
If the keyboard is disabled, physical keyboard actions will be ignored, i.e. they are not passed
on to the system. However, the Keyboard events (like KeyDown) will
still be raised with the SuppressKeyPress property of the event
arguments set to true. To re-enable specific keyboard events, set the
SuppressKeyPress of the event arguments to false.
PauseKey
Gets or sets a key-modifier combination that pauses or resumes the current test execution if pressed.
public static Keys PauseKey { get; set; }
Property Value
Methods
Down(char)
Sets the state of the key associated with the specified character to pressed/down.
public static void Down(char character)
Parameters
charactercharA character associated with a key.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released.
Down(char, bool)
Sets the state of the key associated with the specified character to pressed/down.
public static void Down(char character, bool resetModifiers)
Parameters
charactercharA character associated with a key.
resetModifiersboolTrueto reset the state of modifier keys (e.g. the ShiftKey) to the original state before the call to this method.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released.
Down(Keys)
Sets the state of the specified key to pressed/down.
public static void Down(Keys keyCode)
Parameters
keyCodeKeysThe key.
Remarks
Down(Keys, int, bool)
Sets the state of the specified key to pressed/down.
public static void Down(Keys key, int scanCode, bool setModifiers)
Parameters
keyKeysThe key.
scanCodeintThe hardware scan code for the key. Specify DefaultScanCode to use the default scan code for the specified virtual
key.setModifiersboolA value specifying whether to set the modifiers specified in the
key.
EscapeSequence(string)
Replaces escape characters in sequence by their
escaped representation to be used with the Press(string) method.
public static string EscapeSequence(string sequence)
Parameters
sequencestringAn unescaped sequence of characters.
Returns
- string
An escaped sequence of characters.
Exceptions
- ArgumentNullException
If
sequenceis anullreference.
GetSequenceFromKeyEvents(IList<KeyValuePair<KeyEventArgs, bool>>)
Gets a string that can be passed to Press(string) from a list of key events.
public static string GetSequenceFromKeyEvents(IList<KeyValuePair<KeyEventArgs, bool>> keyEvents)
Parameters
keyEventsIList<KeyValuePair<KeyEventArgs, bool>>A list of key events. Each key event is a pair of KeyEventArgs and a bool that specifies whether the key was pressed (
true) or released (false).
Returns
- string
The key sequence represented as a string.
Remarks
The default upDownEventRearrangeMaxDistance is used.
GetSequenceFromKeyEvents(IList<KeyValuePair<KeyEventArgs, bool>>, int)
Gets a string that can be passed to Press(string) from a list of key events.
public static string GetSequenceFromKeyEvents(IList<KeyValuePair<KeyEventArgs, bool>> keyEvents, int upDownEventRearrangeMaxDistance)
Parameters
keyEventsIList<KeyValuePair<KeyEventArgs, bool>>A list of key events. Each key event is a pair of KeyEventArgs and a bool that specifies whether the key was pressed (
true) or released (false).upDownEventRearrangeMaxDistanceintThe maximum distance between up and down events of a single key to be merged into a key press.
Returns
- string
The key sequence represented as a string.
PrepareFocus(Element)
Tries to set the focus to the specified element if necessary and also tries to make the element visible (if EnsureVisible is specified).
public static void PrepareFocus(Element focusTarget)
Parameters
focusTargetElementThe target element to set the focus to.
Press(Element, string)
Presses the keys specified by sequence.
public static void Press(Element focusTarget, string sequence)
Parameters
focusTargetElementThe element to set the focus to, if possible, before entering the sequence.
sequencestringA sequence of characters and escape groups that specify the keys to press.
Remarks
Each key is repesented by a single character or an escape group. To specify a single keyboard character, use the character itself (e.g. use "aBc" to press the keys A, B+Shift, and C after another). Only the '{' character has a special meaning and needs to be escaped by preceding it with another '{' (specify "{{" to issue a '{' key press).
Escape groups, signalled by braces "{}", may be used to produce a key action with any of the keys specified by the Keys enumeration. "{Z}" means that the 'z' key should be pressed, and "{return}" or "{enter}" that the Return key is to be pressed.
Additionally, in an escape group you can specify a modifier that determines whether the key should be hold down (e.g. {CONTROL down}), released (e.g. {shift up}), or pressed a number of times (e.g. {z 3}). In an escape group, key name and modifier must be separated by a single shift character.
The keys A to Z set the key modifiers (ALT, CTRL, SHIFT) and in particular the shift key depending on their case, even if used in an escape group. If these keys should not modify the shift key state, wrap them into an escape group and add "key" to the character, e.g. "{Rkey}" to press the R key without changing the shift key state.
Press(Element, string, Duration)
Presses the keys specified by sequence.
public static void Press(Element focusTarget, string sequence, Duration keyPressTime)
Parameters
focusTargetElementThe element to set the focus to, if possible, before entering the sequence.
sequencestringA sequence of characters and escape groups that specify the keys to press.
keyPressTimeDurationThe key press time.
Remarks
Each key is repesented by a single character or an escape group. To specify a single keyboard character, use the character itself (e.g. use "aBc" to press the keys A, B+Shift, and C after another). Only the '{' character has a special meaning and needs to be escaped by preceding it with another '{' (specify "{{" to issue a '{' key press).
Escape groups, signalled by braces "{}", may be used to produce a key action with any of the keys specified by the Keys enumeration. "{Z}" means that the 'z' key should be pressed, and "{return}" or "{enter}" that the Return key is to be pressed.
Additionally, in an escape group you can specify a modifier that determines whether the key should be hold down (e.g. {CONTROL down}), released (e.g. {shift up}), or pressed a number of times (e.g. {z 3}). In an escape group, key name and modifier must be separated by a single shift character.
The keys A to Z set the key modifiers (ALT, CTRL, SHIFT) and in particular the shift key depending on their case, even if used in an escape group. If these keys should not modify the shift key state, wrap them into an escape group and add "key" to the character, e.g. "{Rkey}" to press the R key without changing the shift key state.
Press(char)
Presses the key associated with the specified character and releases it again.
public static void Press(char character)
Parameters
charactercharA character associated with a key.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released. The original state of these modifier keys is restored by this method.
Press(char, Duration)
Presses the key associated with the specified character and releases it again after the specified time.
public static void Press(char character, Duration keyPressTime)
Parameters
charactercharA character associated with a key.
keyPressTimeDurationThe time between pressing and releasing the key.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released. The original state of these modifier keys is restored by this method.
Press(char, Duration, bool)
Presses the key associated with the specified character and releases it again after the specified time.
public static void Press(char character, Duration keyPressTime, bool resetModifiers)
Parameters
charactercharA character associated with a key.
keyPressTimeDurationThe time between pressing and releasing the key.
resetModifiersboolTrueto reset the state of modifier keys (e.g. the ShiftKey) to the original state before the call to this method.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released.
Press(char, Duration, int)
Presses and releases the key associated with the specified character for the specified number of times.
public static void Press(char character, Duration keyPressTime, int times)
Parameters
charactercharA character associated with a key.
keyPressTimeDurationThe time between pressing and releasing the key.
timesintThe number of times to press the key.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released. The original state of these modifier keys is restored by this method.
Press(char, Duration, int, bool)
Presses and releases the key associated with the specified character for the specified number of times.
public static void Press(char character, Duration keyPressTime, int times, bool resetModifiers)
Parameters
charactercharA character associated with a key.
keyPressTimeDurationThe time between pressing and releasing the key.
timesintThe number of times to press the key.
resetModifiersboolTrueto reset the state of modifier keys (e.g. the ShiftKey) to the original state before the call to this method.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released.
Press(char, bool)
Presses the key associated with the specified character and releases it again.
public static void Press(char character, bool resetModifiers)
Parameters
charactercharA character associated with a key.
resetModifiersboolTrueto reset the state of modifier keys (e.g. the ShiftKey) to the original state before the call to this method.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released.
Press(string)
Presses the keys specified by sequence.
public static void Press(string sequence)
Parameters
sequencestringA sequence of characters and escape groups that specify the keys to press.
Remarks
Each key is represented by a single character or an escape group. To specify a single keyboard character, use the character itself (e.g. use "aBc" to press the keys A, B+Shift, and C after another). Only the '{' character has a special meaning and needs to be escaped by preceding it with another '{' (specify "{{" to issue a '{' key press).
Escape groups, signaled by braces "{}", may be used to produce a key action with any of the keys specified by the Keys enumeration. "{Z}" means that the 'z' key should be pressed, and "{return}" or "{enter}" that the Return key is to be pressed.
Additionally, in an escape group you can specify a modifier that determines whether the key should be hold down (e.g. {CONTROL down}), released (e.g. {shift up}), or pressed a number of times (e.g. {z 3}). In an escape group, key name and modifier must be separated by a single shift character.
The keys A to Z set the key modifiers (ALT, CTRL, SHIFT) and in particular the shift key depending on their case, even if used in an escape group. If these keys should not modify the shift key state, wrap them into an escape group and add "key" to the character, e.g. "{Rkey}" to press the R key without changing the shift key state.
Press(string, Duration)
Presses the keys specified by sequence.
public static void Press(string sequence, Duration keyPressTime)
Parameters
sequencestringA sequence of characters and escape groups that specify the keys to press.
keyPressTimeDurationThe key press time.
Remarks
Each key is represented by a single character or an escape group. To specify a single keyboard character, use the character itself (e.g. use "aBc" to press the keys A, B+Shift, and C after another). Only the '{' character has a special meaning and needs to be escaped by preceding it with another '{' (specify "{{" to issue a '{' key press).
Escape groups, signaled by braces "{}", may be used to produce a key action with any of the keys specified by the Keys enumeration. "{Z}" means that the 'z' key should be pressed, and "{return}" or "{enter}" that the Return key is to be pressed.
Additionally, in an escape group you can specify a modifier that determines whether the key should be hold down (e.g. {CONTROL down}), released (e.g. {shift up}), or pressed a number of times (e.g. {z 3}). In an escape group, key name and modifier must be separated by a single shift character.
The keys A to Z set the key modifiers (ALT, CTRL, SHIFT) and in particular the shift key depending on their case, even if used in an escape group. If these keys should not modify the shift key state, wrap them into an escape group and add "key" to the character, e.g. "{Rkey}" to press the R key without changing the shift key state.
Press(Keys)
Presses the specified key and releases it again.
public static void Press(Keys keyCode)
Parameters
keyCodeKeysThe key.
Remarks
Press(Keys, Duration)
Presses the specified key and releases it again after the specified time.
public static void Press(Keys keyCode, Duration keyPressTime)
Parameters
Remarks
Press(Keys, Duration, int)
Presses and releases the specified key for the specified number of times.
public static void Press(Keys keyCode, Duration keyPressTime, int times)
Parameters
keyCodeKeysThe key.
keyPressTimeDurationThe time between pressing and releasing the key.
timesintThe number of times to press the key.
Remarks
Press(Keys, int, Duration, int, bool)
Presses and releases the specified key for the specified number of times.
public static void Press(Keys key, int scanCode, Duration keyPressTime, int times, bool setModifiers)
Parameters
keyKeysThe key.
scanCodeintThe hardware scan code for the key. Specify DefaultScanCode to use the default scan code for the specified virtual
key.keyPressTimeDurationThe time between pressing and releasing the key.
timesintThe number of times to press the key.
setModifiersboolA value specifying whether to set the modifiers specified in the
key.
Set(char, bool)
Sets the state of the key associated with the specified character to either pressed/down or released/up.
public static void Set(char character, bool down)
Parameters
charactercharA character associated with a key.
downboolTrueto press the key,falseto release it.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released. The original state of these modifier keys is restored by this method.
Set(char, bool, bool)
Sets the state of the key associated with the specified character to either pressed/down or released/up.
public static void Set(char character, bool down, bool resetModifiers)
Parameters
charactercharA character associated with a key.
downboolTrueto press the key,falseto release it.resetModifiersboolTrueto reset the state of modifier keys (e.g. the ShiftKey) to the original state before the call to this method.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released.
Set(Keys, bool)
Sets the state of the specified key to either pressed/down or released/up.
public static void Set(Keys keyCode, bool down)
Parameters
Remarks
Set(Keys, int, bool, bool)
Sets the state of the specified key to either pressed/down or released/up.
public static void Set(Keys key, int scanCode, bool down, bool setModifiers)
Parameters
keyKeysThe key.
scanCodeintThe hardware scan code for the key. Specify DefaultScanCode to use the default scan code for the specified virtual
key.downboolTrueto press the key,falseto release it.setModifiersboolA value specifying whether to set the modifiers specified in the
key.
ToKey(string)
Converts the specified string representation (see ToString(Keys)) to a Keys value.
public static Keys ToKey(string key)
Parameters
keystringThe string representation of a key.
Returns
- Keys
The key value.
Exceptions
- ArgumentException
If the string representation of the key is invalid.
ToString(Keys)
Returns a string representation of the specified key.
public static string ToString(Keys key)
Parameters
keyKeysA key.
Returns
- string
A string representation of the key.
Up(char)
Sets the state of the key associated with the specified character to released/up.
public static void Up(char character)
Parameters
charactercharA character associated with a key.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released. The original state of these modifier keys is restored by this method.
Up(char, bool)
Sets the state of the key associated with the specified character to released/up.
public static void Up(char character, bool resetModifiers)
Parameters
charactercharA character associated with a key.
resetModifiersboolTrueto reset the state of modifier keys (e.g. the ShiftKey) to the original state before the call to this method.
Remarks
This method may cause modifier keys (like ControlKey or ShiftKey) to be pressed or released.
Up(Keys)
Sets the state of the specified key to released/up.
public static void Up(Keys keyCode)
Parameters
keyCodeKeysThe key.
Remarks
Up(Keys, int, bool)
Sets the state of the specified key to released/up.
public static void Up(Keys key, int scanCode, bool setModifiers)
Parameters
keyKeysThe key.
scanCodeintThe hardware scan code for the key. Specify DefaultScanCode to use the default scan code for the specified virtual
key.setModifiersboolA value specifying whether to set the modifiers specified in the
key.
Events
KeyDown
Occurs when a key is pressed or hold down.
public static event KeyEventHandler KeyDown
Event Type
KeyUp
Occurs when a key is released.
public static event KeyEventHandler KeyUp