Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript simulate key press

// Type a character
function typeChar(c) {
  document.dispatchEvent(
    new KeyboardEvent('keydown', {
      keyCode: c.charCodeAt(0),
      which: c.charCodeAt(0),
      key: c
    })
  );
}

// Type a word
function typeWord(w) {
  for (let c of w) {
    typeChar(c);
  }
}

typeWord('hello world');
Comment

how to simulate a keypress in javascript

window.addEventListener('keydown', (e) => {
  console.log(e)
})

window.dispatchEvent(new KeyboardEvent('keydown', {
  'key': 'a'
}));
//use --> document.addEventListener('keypress', (event)=>{console.log(event)}) <-- copy the output of the key you want
//and place it (as object) at ...ardEvent('keydown', HERE)...
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular material change placeholder color 
Javascript :: window.open javascript auto close 
Javascript :: how to copy text in react 
Javascript :: javascript get unique values from array 
Javascript :: reset date input javascript 
Javascript :: react split array into chunks 
Javascript :: MIN_SAFE_INTEGER 
Javascript :: javascript loop through arrya 
Javascript :: jquery click event 
Javascript :: js self executing anonymous function 
Javascript :: javascript loop through object values 
Javascript :: scrolltop top to bottom body get count 
Javascript :: usehistory not found in react-router-dom 
Javascript :: convert string to array in vue js 
Javascript :: jquery each data 
Javascript :: React CKeditor Upload Adapter 
Javascript :: modify root in javascript 
Javascript :: append to array check if exists javascript 
Javascript :: remove menu bar electron 
Javascript :: run javascript from uri 
Javascript :: js function return fetch result 
Javascript :: react or vue 
Javascript :: randomColor 
Javascript :: codewars js Find the first non-consecutive number 
Javascript :: end code nodejs 
Javascript :: nestjs cli create project 
Javascript :: npm react router dom 
Javascript :: unpack array into variables javascript 
Javascript :: add font awesome to vue 
Javascript :: play a sound wiith js 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =