20 Feb 2024 08:40 AM - edited 20 Feb 2024 08:44 AM
For some applications, keyboard actions are needed, such as arrow up, arrow down, arrow left, arrow right, enter, escape etc.
Issue | Solution | Tasks | Alternative(s) |
---|---|---|---|
Keyboard action needed | Use Keydown & keyup events | Find key codes & use snippet | create ticket if that doesn't work |
Where an application needs a keyboard button to be pressed, we can use a JavaScript event to perform this.
var input = document.querySelector("body");
Go to the relevant page on Chrome browser
Right click on the element which you want to interact with on the page-> Inspect the element which will open the developer tools
Update the input variable with the selector you have just copied
var input = document.querySelector("#yourSelector");
var parameters1 = {isTrusted: true, altKey: false, bubbles: true, cancelBubble: false, cancelable: true, charCode: 0, code: "F7", composed: true, ctrlKey: false, defaultPrevented: true, detail: 0, eventPhase: 0, isComposing: false, key: "F7", keyCode: 118, location: 0, metaKey: false, repeat: false, returnValue: false, shiftKey: false, which: 118};
var parameters2 = { isTrusted: true, altKey: false, bubbles: true, cancelBubble: false, cancelable: true, charCode: 0, code: "F7", composed: true, ctrlKey: false, defaultPrevented: false, detail: 0, eventPhase: 0, isComposing: false, key: "F7", keyCode: 118, location: 0, metaKey: false, repeat: false, returnValue: true, shiftKey: false, which: 118};
var input = document.querySelector("#yourSelector");
input.dispatchEvent(new KeyboardEvent("keydown", parameters1));
input.dispatchEvent(new KeyboardEvent("keyup", parameters2));
You will need to update the following: