override browser shortcuts

0 favourites
  • 5 posts
From the Asset Store
This is a single chapter from the Construct Starter Kit Collection and the Student Workbook from the Workshop.
  • Hello,

    I want to use shortcuts like ctrl+d ant some stuff in my current project. Unfortunately, this triggers some browser shortcuts. Firefox and Chrome do not profide any options to disable or customize their shortcuts.

    Do someone have an answer to this ussue ?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Currently there is nothing built in Construct to prevent the default behaviour of the browser, but you can use a small script to do it. At the start of your event sheet add this script.

    // Listen for the document's "keydown" event
    document.addEventListener("keydown", (event) =>
    {
    	// If ctrl + H is pressed...
    	if (event.ctrlKey && event.code === "KeyD")
    	{
    		// then prevent the browser's default behaviour
    		event.preventDefault();
    	}
    }, true);
    

    That should do the trick.

    Here is a list of the names for keys if you want to add other shortcuts you would like to disable

    https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values

    An additional note, if you end up using this, you have to make sure that the script is only executed once. Otherwise each time the event sheet that has this script will add a new event listener.

  • Thank you !

    Unfortunately this trick is not working, but maybe i am doing it wrong. I used "add script", then copy/paste your script in it, and i put it at the very top of the event sheet. I also tried to put it in an action, under an "On start of Layout" condition.

  • It didn't work?

    I tried it out in preview mode, and when the preview window has focus the shortcut does not work. It does work if the preview window doesn't have focus.

    Here is the test project https://www.dropbox.com/s/6scfsvwism7tmwk/PreventKeyboardShortcut.c3p?dl=0

    Mind you, this won't prevent the default behavior on the editor itself, only on the window the project is running on, be it the preview window or where ever the exported game ends up running.

  • After several tests, it seems that the shortcut ctrl+H only works when i launche a preview in a new tab. When the preview is launched in a new browser window, the shortcut ctrl+H seems to have no effect on the preview window, but this is independant from your script : if i disable the script, this shortcut remains inactive on the preview window. This is probably inherant to this particular shortcut.

    Shortcuts like ctrl+D or ctrl+S will still remains active on the preview window (or preview tab, i tested both), even if i put "KeyD" instead of "KeyH" in the script in your test project.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)