Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect when exiting fullscreen is triggered by ESC key press #179

Open
matcho opened this issue Jul 21, 2020 · 6 comments
Open

Detect when exiting fullscreen is triggered by ESC key press #179

matcho opened this issue Jul 21, 2020 · 6 comments

Comments

@matcho
Copy link

matcho commented Jul 21, 2020

Hello,

When binding a function to "change" event, through .on() method, how is it possible to detect either :

  • whether this change event was triggered by pressing ESC key
  • whether the browser is entering or exiting fullscreen mode

I couldn't find such information in the event object.

What I'm trying to achieve is to detect when the browser is exiting fullscreen mode because ESC key was pressed.

Thanks for any help, screenfull is great,
Mathias

@sindresorhus
Copy link
Owner

whether this change event was triggered by pressing ESC key

I don't think browsers expose this information at all.

whether the browser is entering or exiting fullscreen mode

This could possibly be added.

@h3rb
Copy link

h3rb commented Nov 19, 2020

perhaps you can test for the ESC key

(window.addEventListener( "onkeyup", ... )

there is also https://stackoverflow.com/questions/10706070/how-to-detect-when-a-page-exits-fullscreen

@chenxiaoyao6228
Copy link

any workarounds ?

@Dhruvinhp
Copy link

// you can use it like this,
const escFunction = useCallback((event) => { if (event.keyCode === 27) { console.log("----logic----") } }, []); useEffect(() => { document.addEventListener("keydown", escFunction); }, []);

// And don't forget to import useCallback from react, hope this helps ;)

@surdu
Copy link

surdu commented May 16, 2022

whether the browser is entering or exiting fullscreen mode

const isFullscreen = !!document.fullscreenElement;

You could use this inside your change handler.

@gurvirbaraich
Copy link

You could check for ESC key within your change handler

class FullScreenExitHandler {
    static ESCKeyPressed = false;

    onFullScreenChange = () => {
        /** Full Screen Change Logic */

	if (FullScreenExitHandler.ESCKeyPressed) {
		/** ESC Key Pressed was used to exit fullscreen */
	}

	/** Setting ESC key staus to default */
	    FullScreenExitHandler.ESCKeyPressed = false;
    };
}

/** Checks for keypresses on the dom */
window.addEventListener("keydown", function (event) {
    if (event.keyCode === 27) {
	FullScreenExitHandler.ESCKeyPressed = true;
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants