question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useHotKeys inside Iframe

See original GitHub issue

Hi there!

version: react-hotkeys-hook@4.0.2

I’m trying to use useHotKeys inside react-frame-component Iframe, but when i’m focusing on iframe and pressing some hotkeys, events doesn’t work, nothing happens, example:

import React, { memo, useRef, useEffect, useCallback } from 'react'

import Frame, { FrameContextConsumer, useFrame } from 'react-frame-component'

import {useHotkeys} from 'react-hotkeys-hook'

const InnerComponent = () => {

	const { document, window } = useFrame();

	useHotkeys('s', (event) => {
		console.log('s pressed');
	})

	return null;
};

const Editor = () => {
	return (
		<>
		 	<Frame>
				<InnerComponent/>
			</Frame>
		</>
	)
}
export default memo(Editor)

what i’m doing wrong, or maybe i need to pass some arguments to useHotkeys function?

thanks!

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mbrevdacommented, Dec 6, 2022

Had similar issues with other libs when using React Portals (like react-helmet and many css-in-js libs that want to access <head> and all assume that <head> is located at document.getElementsByTagName('head')[0]).

It boils down to listening on the wrong document (i.e. the parent document, where react lives, instead of listening on the iframe’s document). It’s likely that being able to pass document to the hook will resolve the issue

1reaction
JohannesKlausscommented, Dec 25, 2022

Starting with 4.3.0 you can set a different document to bind to in the useHotkey hook like so:

import FrameComponent from 'react-frame-component'

const InsideFrameComponent = () => {
  const { document } = useFrame()

  useHotkeys("s", () => console.log("I am triggered inside an iframe"), { document })

  return <div>....</div>
}

function App() {
  return (
    <FrameComponent>
      <InsideFrameComponent/>
    </FrameComponent>
  )
}

Thanks to @mbrevda for the pointer to the solution!

Read more comments on GitHub >

github_iconTop Results From Across the Web

useHotkeys from within an iframe · Issue #4731 - GitHub
I am trying to get hotkeys working with a BlueprintJS app residing within an iframe (via react-frame-component ). In the code sandbox above,...
Read more >
Using jQuery hotkeys from within a blurred iFrame
I'm using jQuery Hotkeys within an iframe (iFrame A), on a page with two iFrames (A and B). When the focus is on...
Read more >
Can't use hotkeys defined for page in UI for ASP.NET AJAX
Hello, The content area of RadEditor is by default rendered as an iframe element (ContentAreaMode="iframe"), which is another document which ...
Read more >
Iframes - Minimal Documentation
Use Hotkeys to adjust iframes globally: Cycle between iframe width options. Helper classes.
Read more >
How to execute the keyboard shortcuts when Focus is ... - MSDN
If iFrame is having the focus then i am not able generate the Keyboard shortcut alerts. May i know how to implement? I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found