Key.chord or ActionSequence? - javascript

I need to simulate pressing CTRL + C, CTRL + V, and so on. Selenium give us two way to do it:
webdriver.Key.chord(var_args), example:
webdriver.Key.chord(webdriver.Key.CONTROL, 'c')
and Class ActionSequence, example:
new webdriver.ActionSequence(driver).keyDown(webdriver.Key.CONTROL)
.sendKeys('с')
.keyUp(webdriver.Key.CONTROL);
action.perform();
Which way is better to use and why?

Firstly, webdriver.Key.chord doesn't send any events, it simply appends the arguments and adds a NULL on the end. According to the code:
Note: when the low-level webdriver key handlers see Keys.NULL, active
modifier keys (CTRL/ALT/SHIFT/etc) release via a keyup event.
So assuming you pass the resulting string to elem.sendKeys(), you'll end up with:
sendKeys: Ctrl-c
keyUp: Ctrl
By contrast, using the Actions API in your example, you would get:
explicit keyDown: Ctrl // *Not* released
sendKeys: c
explicit keyUp: Ctrl // Note original edit had 'keyDown'
Which should give exactly the same result.
I'm sure webdriver.Key.chord is a more natural way to express the use of modifier keys, without anyone needing to think about key downs or ups, and ease of use is paramount. Even if 'performance' was slightly different, the difference isn't worth thinking about.

Related

In Intern.js Leadfoot, how do I preform a CTRL Click

I'm just wondering what the preferred way to preform a ctrl + click action in leadfoot is. In java I would have used the Actions class and used keyDown, but since we have moved to a JS based framework I'm a complete fish out of water!
I've seen in the api that there is a pressKeys function but it doesn't seem to do what we need. I've thought about using jQuery to do this but I would really rather keep it in the current framework.
Any help greatly appreciated.
Peter
You can use pressKeys, for example:
command.moveMouseTo(myBtn)
.pressKeys(keys.CONTROL)
.clickMouseButton()
.pressKeys(keys.CONTROL)
A good thing to remember about pressKeys (https://theintern.github.io/leadfoot/Command.html#pressKeys)
keys: The text to type in the remote environment. It is possible to type keys that do not have normal character representations (modifier keys, function keys, etc.) as well as keys that have two different representations on a typical US-ASCII keyboard (numpad keys); use the values from leadfoot/keys to type these special characters. Any modifier keys that are activated by this call will persist until they are deactivated. To deactivate a modifier key, type the same modifier key a second time, or send \uE000 ('NULL') to deactivate all currently active modifier keys.
TheIntern/LeadFoot provides you a function execute. You can trigger any event from this function using JS.
.execute(function() {
//You can even access window from here
$("#someId").click() //example
//or try something like this
e = jQuery.Event("keydown");
e.which = 50;
e.ctrlKey = true;
$("input").trigger(e);
})
To trigger keyevent follow these links:
jquery trigger ctrl + click
How to trigger key combo with jQuery

how to handle non english key presses

When I switch my keyboard layout to hebrew and press a character, it is received in the kepress event as is. For example, clicking ה (the v key), then e.which is 1492. However, when I do a combination the key is the english key. So for alt+ה e.which is 86. So the event looks like alt+v
This is a pain if I want to create a function that accepts key combinations and callbacks and registers the callbacks but also shows a documentation of the callbacks, since if I register alt+ה, then when I press the combination it will look like alt+v and it wouldn't match the registered combinations. But if I register alt+v, then the documentation will be awkward.
So my question boils down to whether one of the following is possible:
knowing which key actually was pressed (meaning, knowing that ה was pressed together with alt and not v)
mapping between the hebrew characters (or any non-english) to their physical english counterparts, so when my function receives alt+ה it will convert it to alt+v for the callback lookup.
Of course I want something generic, that will work for any language, not list just the hebrew alphabet.
You should not care if it is alt-ה or alt-v, you want to perform the same operation on both cases.
If it is not identified, it is not identified anywhere, so your function will get alt-v as well.
if your function gets something ("Alt", "ה") you should create an object for conversion.
var conv= { 'ה': 'v'; 'ש': 'a',...}

Javascript Keyup isn't called when Command and another is pressed

This is Mac only problem; I've tried this on windows and it works fine.
I have a script that saves which keys are pressed on keydown and deletes them on keyup.
$(function(){
var keys = [];
$(document).keydown(function(event) {
keys[event.which] = true;
});
$(document).keyup(function(event) {
delete keys[event.which];
console.log(keys);
});
});
All I am doing right now is console logging whatever is left after the keyup, which should be nothing. This works as expected when you press any number of keys simultaneously.
HOWEVER, when you press command and any other key, the other key is NOT released! So console.log will show that key to be true. This will remain true until you press that key itself.
This only happens on a Mac, and it only happens when one of the keys pressed is the Command key. Here is a very simple plunker with the above example.
Any ideas?
The Mac is modifying your key whenever you press Command, and thus the normal keyup event never fires.
C = command key and K = non-command key
As you press C and K, they register normally. While they are both simultaneously pressed, the Mac captures K and modifies it. In modifying K, the Mac somehow makes K's keyup event not fire as it is supposed to. C's keyup works as expected, however.
Since K's keyup never actually fires, it won't correctly delete the matching element from keys. Later on when you press K without C, the keydown event overwrites the existing keydown in keys. And when K's keyup correctly fires, it works as expected.
In addition to all the normal keys used to input ASCII characters,
keyboards typically have many special purpose keys that do other
things. These do not necessarily generate the same events as normal
keys, and they show less consistency across browsers.
JavaScript Madness: Keyboard Events. Potentially helpful article for all key-related problems.
I can't fix this Mac issue, but here is my way of getting around it.
This answer will help you if you are trying to have keyboard-shortcut behavior, where the user presses CMD+S to do save, or something like that. This answer does not apply to people who may be building a game or something where their keyboard's keydown states need to be known at each run frame. Sorry!
In the KeyboardEvent returned by keydown, you can do the following
$(document).keydown(function(keyboardEvent) {
if (keyboardEvent.metaKey){
// on Mac, CMD is down ...or Meta key is down on pc
console.log(keyboardEvent.meta + " & command key are down")
switch (keyboardEvent.which) {
...
}
}
});
If your keyboard shortcut overlaps with the browser's, you need to make sure to cancel the propagation of the keyboard event,
keyboardEvent.preventDefault()
I hope this helps people who want keyboard shortcut functionality that is Mac compatible!

Caret position not keeping track?

What I'm trying to do is put key codes in an array, to do some interesting stuff with later on. So, I capture the keystrokes, get the caret position and put the key code in the array (with some help from MooTools):
var keyArray = [];
$('form').addEvent('keyup', function(event) {
var pos = document.activeElement.getCaretPosition();
keyArray[pos] = event.code;
});
Generally speaking, this works great. However, I noticed some undefined values in my array when showing the complete array in my console. Exploring this further, I found out that when typing quickly, the caret position seems to lose track, or being quick/slow to respond. I've made a jsfiddle to demonstrate this: http://jsfiddle.net/HQVR8/1/
If you type quickly in this example, you'll see a caret position sequence like
- 1 - 2 - 3 - 5 - 6 - 6.
But when typing slowly, it's
- 1 - 2 - 3 - 4 - 5 - 6.
Of course, the trouble now when typing quickly is that I have a undefined value in my array and I overwrite one array item. So the result is different.
My question is if I can somehow let the caret position keep track. I've tried using the 'native' selectionStart instead, but results are the same. I also tried capturing the caret position in a keydown event and putting it in an array in the keyup event. No difference. I'm wondering if using little pauses (ie. forcing the user to type slower) might solve it, but this feels like a hack and I would prefer a 'proper' solution. Hopefully, there is one.
you are basically making a mess by using an array instead of an object.
array indexes are dodgy and can create sparse arrays if you are not careful. for instance:
var foo = [];
foo[0] = "one!"; // foo.length = 1;
foo[2] = "two!"; // foo.length = 3, foo[1] = undefined
so if you type too fast and skip the return value, it is probably doing just that. also, pasting etc can push caret further down...
you can probably use an object though there are no guarantees on the order of keys in vs keys out in all browsers - particularly webkit, who tend to reorder and put numeric keys at top of object key loops... but if you prefix the key like "pos" + caretIndex, you ought to get FIFO
one way to solve your need to extract the actual codes w/o the undefined is via Array.filter.
eg.
retArray = Array.filter(retArray, function(el) {
return el !== undefined;
});
With an object, you can just do:
console.log(Object.values(foo));
After some more tests, it appears to be a behavior specific to keyup. When I use keydown I do get a consistent sequence: http://jsfiddle.net/HQVR8/3/
One disadvantage is that keydown is a step behind keyup when you're doing the 'value collecting' I'm doing, but in my setting, this is only a minor issue.
The difference in behavior appears odd to me: when you press four keys at a time, the keyup displays the same caret position for all of them, while keydown is showing four separate caret positions. This seems odd because you press them at once, but also depress them at once, so the caret 'readings' should be the same.
go to the JSfiddle and fiddle with this:
a) depress 'q', then depress 'w', then release 'q', then release 'w' (fast enough to avoid autorepeat). That kind of sequence happens quite often when you type 'quickly' with more than one finger :).
b) leave any key depressed long enough for the autorepeat to kick in.
The differences are in plain view
Basically, keyUp fires when the physical key is released, but
another key can be depressed before the previous one is released
keyDown is called in case of autorepeat, while keyUp isn't
Since carret position and whatever text area updates are kept in sync with keyDown, you will simply miss the intermediate steps when monitoring keyUp.
In case (a), the keyUp handler sees carret pos jump from 0 to 2 as 'q' is released because two characters have been typed before the first key was released. The release of 'w' does not change carret position, so the handler ends up storing both q and w at position 2.
In case (b), the handler misses the repeats completely, and stores only the last instance of the key.
As a conclusion, using KeyUp will never achieve the intended meaning.
keyDown could, but personnally I would rather hook the changed event.
I reckon it is no less reliable to detect a bot filling the field (after all, a legit user could want to paste the password too), and you won't have to bother with control keys like backspace or whatever other non-keyboard means of clearing the input.
You could use keyUp as an extra paranoid check, as long as you don't expect to reconstruct the exact input. For instance, you could simply check that the released key matches the character at current cursor position. But frankly I'm not sure it is worth the bother.

How to set a hotkey combination of 2 letters with jQuery hotkey.js?

I've use jQuery hotkey.js plugin in my application to support shortcut keys.
But, currently seems it only support key combinations like "ctrl+a","shift+a","alt+a"... I've tried
$(document).bind('keypress','g+g',fn);
or
$(document).bind('keypress','g,g',fn);
or even
$(document).bind('keypress','g','g',fn);
but non of them work.
My question would be what can I do to support this kind of hotkeys?
Thank you very much.
Shortcut.js only supports a modifier key (alt, ctrl, meta, shift) + another key. It does not support multiple non-modifier keys (e.g r+g, or even ctrl+e+f (proof case)).
Have a look at the linked answer for another method to achieve the desired effect.
SO answer: Listen to multiple keydowns

Categories

Resources