1

I have a javascript function that gets called from an aspx.vb file:

using ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "pressKey();", True)

and I need the pressKey function to simulate pressing the END key as if the user pressed it on their keyboard.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
William V.
  • 343
  • 1
  • 13
  • By e.keycode and match the syn event. – TGarrett Nov 29 '18 at 21:53
  • possible duplicate of https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically – Naga Sai A Nov 29 '18 at 22:14
  • Possible duplicate of [Is it possible to simulate key press events programmatically?](https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically) – imtheman Nov 30 '18 at 00:27

1 Answers1

1

The charCode for the End key is 35. CSS tricks has a good list of character codes: https://css-tricks.com/snippets/javascript/javascript-keycodes/

Here is the Vanilla JS solution:

var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? 
"initKeyboardEvent" : "initKeyEvent";


keyboardEvent[initMethod](
               "keydown", // event type : keydown, keyup, keypress
                true, // bubbles
                true, // cancelable
                window, // viewArg: should be window
                false, // ctrlKeyArg
                false, // altKeyArg
                false, // shiftKeyArg
                false, // metaKeyArg
                35, // keyCodeArg : unsigned long the virtual key code, else 0
                0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);
Neil VanLandingham
  • 1,016
  • 8
  • 15