Hi guys, how I can get currently pressed keyboard while I’m in EditorWindow
?
Here’s my latest code (but didn’t work, Event.current
is always null) :
void Update(){
if(Event.current!=null && Event.current.isKey && con.Listen ())
Repaint ();
}
public bool Listen(){
switch(Event.current.keyCode){
case KeyCode.DownArrow:
// ...
break;
case KeyCode.UpArrow:
// ...
break;
default:
break;
}
return c;
}
sorry, I never know that it will works while I’m inside OnGUI
void OnGUI(){
if(Event.current!=null && Event.current.isKey && con.Listen ())
Repaint ();
}
So I came across the same issue and discovered a different way to do make a editor window respond to keydown or keyup event, without using OnGUI() method.
If you are using UIToolkit building the Editor Window, you can use RegisterCallback.KeyDownEvent / RegisterCallback.KeyUpEvent on the rootVisualElement of the window.
Sample code is like this: (Write this inside your editor window class)
rootVisualElement.RegisterCallback<KeyDownEvent>(evt =>
{
if (evt.keyCode == KeyCode.'Your desire keyCode')
{
Debug.Log("Do something");
}
});
Read more: UIElements.KeyDownEvent - Unity スクリプトリファレンス
Hope this helps.