r/learnreactjs • u/dog_superiority • Aug 14 '22
Question onKeyDown is not working for me.
I want to get an event when the user hits the CTRL button. However, I am not getting any events. If I press CTRL nothing happens, and when I press any other key, it just shows up in the debug console. What am I missing??
const MyComponent = (...) => {
...
const keyDown = (e) => {
console.log("keyDown!");
}
const generateDOM = layoutState => {
return (
<div style={{ width: "100%", height: "100%"}}
onKeyDown={keyDown}
onMouseMove={mouseMoved}
onMouseDown={mouseDown}
onMouseUp={mouseUp}>
{generateContent(...)}
</div>);
}
}
3
Upvotes
2
u/oze4 Aug 14 '22
You should be able to achieve this by setting the
tabIndex="0"prop on the div.Like this:
https://codepen.io/oze4/pen/mdxzxWp?editors=1010
1
u/ikeif Aug 14 '22
So, the user has clicking on content inside of that div, and it's logging
keyDown!for everything except thectrlkey?What do the elements look like in
generateContent? Do they have events? Are you returning false? Doing anything inside there?This simply is not enough code/detail to assist in diagnosing.