Skip to content

Instantly share code, notes, and snippets.

Last active May 5, 2022 17:27
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save stefansundin/65e3d6db697636d8e7f1 to your computer and use it in GitHub Desktop.
Makes the YouTube shortcuts work even if the player is not focused. Install Greasemonkey/Tampermonkey first, then click [Raw] to install.
// ==UserScript==
// @name YouTube global shortcuts
// @namespace
// @homepage
// @downloadURL
// @version 1.1
// @author Stefan Sundin
// @description Makes the YouTube shortcuts work even if the player is not focused.
// @icon
// @match*
// ==/UserScript==
// Space = Pause/play
// F = Toggle fullscreen
// M = Toggle mute
// P = Previous video (if in playlist)
// N = Next video (if in playlist)
// left and right keys focuses the button, but the first keydown won't trigger seeking unfortunately
// P and N already works without this userscript, but you must hold shift.
// Use Ctrl+Space to scroll down, Shift+Space to scroll up
window.addEventListener('keydown', function(e) {
var button = document.getElementsByClassName('ytp-play-button')[0] || document.getElementById('movie_player');
if (!button) return;
// Skip if in comment reply form, or search field
if (['TEXTAREA','INPUT'].indexOf(e.srcElement.tagName) !== -1 || ['yt-commentbox-text','yt-simplebox-text'].indexOf(e.srcElement.className) !== -1) return;
var c = String.fromCharCode(e.keyCode).toLowerCase();
if (c == ' ' || e.keyCode == 37 || e.keyCode == 39) { // 37 = left key, 39 = right key
if (c == ' ' && (e.shiftKey || e.ctrlKey)) return;
if (e.srcElement != button) {
if (c == ' ');
else if (c == 'n') {
else if (c == 'p') {
Copy link

You're the bomb, man. I found a few older scripts that weren't working so I dug into the markup a bit and noticed the individual buttons had ids, which Googling one of them led me here! Saved me a bit of time, thank you.

Copy link

Delicious! I use it in conjunction with Autohotkey to control the player without it having focus. and your script gives me access to everything. Speed control, play control, skipping, you name it - that was previously unattainable because the player always needed focus. You, SIr, are great!

Copy link

Note: I am not currently using this script, so it might not work perfectly anymore.

But it looks like someone forked it and made some changes, so maybe this one works better if you have having issues:

Copy link

danklimkowski commented Nov 9, 2017

I'm having trouble getting this to work. Can you please help me figure this out?

I downloaded Tampermonkey and installed it with the Raw button and tampermonkey says that the script is on when i visit a youtube page. But i dont see how i can program/use keyboard shortcuts. I tried the keyboard shortscuts listed in the script, but only the default keys are working when youtube is open. Is there somewhere that i can edit the different keyboard shortcuts? Is there somewhere in the script i need to edit? Or a setting in tampermonkey? I also use Keyboard Maestro, would that be helpful here?

Thanks, Dan

Copy link

@DanKlim Sorry, I'm not using this anymore, and since they've redesigned YouTube several times, I don't think it works anymore.

I suggest that you check if the classes are the same, and try to use debug console.log statements to see what parts of the code runs. Hope you can get it working!

Copy link

Can you make scripts that set a timer to automatically pause/stop on all Youtube embed videos? Like pause a video after X seconds!!?

Copy link

For those who want a full keyboard navigation on YouTube check my Chrome extension:

Copy link

Man, you are a legend, thank you for making this. Can't believe Google is so bad at not implementing this.

Copy link

Disclaimer: I have not used this for years and I doubt it still works. I think YouTube does have some global shortcuts these days, like the space key.


Copy link

Yeah, thank you for the warning. What I wanted was a little simpler so I didn't use your code but learned from it. 🙏🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment