# How to detect whether a key on the keyboard is pressed when the mouse cursor is hovering over a subwidget

**URL:** <https://forums.unrealengine.com/t/how-to-detect-whether-a-key-on-the-keyboard-is-pressed-when-the-mouse-cursor-is-hovering-over-a-subwidget/1358955>\
**Category:** Blueprint\
**Tags:** mouse-cursor, unreal-engine, keyboard, Widget, question, Blueprint\
**Created:** [November 3, 2023, 5:49am UTC](https://forums.unrealengine.com/t/how-to-detect-whether-a-key-on-the-keyboard-is-pressed-when-the-mouse-cursor-is-hovering-over-a-subwidget/1358955 "2023-11-03T05:49:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bungeen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bungeen](https://forums.unrealengine.com/u/Bungeen)\
**Post date:** [November 3, 2023, 5:49am UTC](https://forums.unrealengine.com/t/how-to-detect-whether-a-key-on-the-keyboard-is-pressed-when-the-mouse-cursor-is-hovering-over-a-subwidget/1358955/1 "2023-11-03T05:49:54Z")

</div>

Hello!  
I created a widget for a separate slot. Then I created a widget of a separate component from these slots. And then I use these components to compose the main widget. I want to create an event which be triggered in the widget slot when I hover over it with the mouse and press some key on the keyboard (for certainty, let it be ‘E’)  
I assume that this is done through the OnKeyDown event, but it didn’t work out for me. (I was trying to just output a message).  
Honestly, I do not know which images I can attach to the question. Therefore, if something is required, I am ready to attach it.

Updated:  
I tried checking constantly to see if IsHovered is True. And if so, then set the focus on this widget component

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/7/0/570859e9de6f57c629f957f8bcc44ff06ffd5170.png)  
But I use about a hundred of these elements. And I am confused by the performance of this solution

Updated\_Update:  
So, I discovered “Event On Mouse Entering” and “Event On Mouse Leaving”  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/5/2/b52650440f063497f84adcfab3dc4e48bd86c94e.png)  
I change focus to widget when I entering and change focus to Viewport when I leaving

But I have a problem. This widget is hidden or shown when you press a button on the keyboard. And this is detected using the Enhanced Input Action in BP\_Player. But with this implementation, when pointing at the slot widget, the input to BP\_Player is disabled. So I this

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/e/8/4e8a75e92d9187d9f43538c941987e4f049acd6f.png)  
In BP\_Player, I created an additional event, which I tied to the same logic as EnchancedInput. And I call this event when pressing the same button, but being in the focus of the widget

I don’t particularly like this realization. Is there a way to do otherwise? And also is it possible to translate OnKeyDown to EnhancedInput in this situation?

---

<div class="post-metadata">

**Author:** ![eldany.uy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eldany.uy/32/821920_2.png) [@eldany.uy](https://forums.unrealengine.com/u/eldany.uy)\
**Post date:** [November 3, 2023, 4:36pm UTC](https://forums.unrealengine.com/t/how-to-detect-whether-a-key-on-the-keyboard-is-pressed-when-the-mouse-cursor-is-hovering-over-a-subwidget/1358955/2 "2023-11-03T16:36:09Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/1/e/61e3f1dfc10a3620770e63f913c630b1ac6dbc54.png)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/6/c/86c22d2eb4941f88613574b5935d732caa81b221.png)

---

<div class="post-metadata">

**Author:** ![Bungeen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bungeen](https://forums.unrealengine.com/u/Bungeen)\
**Post date:** [November 3, 2023, 7:32pm UTC](https://forums.unrealengine.com/t/how-to-detect-whether-a-key-on-the-keyboard-is-pressed-when-the-mouse-cursor-is-hovering-over-a-subwidget/1358955/3 "2023-11-03T19:32:53Z")

</div>

Ok. Thanks for the advice!  
But Is there a critical difference with my first solution to the problem? And also, isn’t Event Tick too unoptimized?
