skip to Main Content

I want to use AddEventListener in javascript to reload the page when i press Enter(Like what F5 automatically do), and overall I want to know how to manipulate the shortcuts that being already defined in the computer(Like F5) using JavaScript.
thanks for help

I tried to use Addeventlistener but i couldn’t specify it to reload the page

2

Answers


  1. Try this:

    document.addEventListener("keyup", (event) => {
     if (event.key === 'Enter') {
          location.reload(true)
        }
    });
    
    Login or Signup to reply.
  2. Instead of using "keyup," use the "keydown" event in the addEventListener. Each key has a unique "key code," for example, F5 is associated with the key code 116. Check the condition to determine whether the pressed key has a key code of 116 or not.

    document.addEventListener("keydown",(e)=>{
               if(e.keycode === 116){
                 location.reload(true);
               }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search