Best method to trap keyboard event in editor mode...

Hello all,

I’m trying to trap keyboard events in a new script I’m writing while extending the Unity editor with my own editor and inspector.

I’ve tried all kinds of permutation of things based on ‘Event.current.xxx’ and/or Input.xxx from within calls such as OnGui(), OnSceneGui() and OnInspectorGui() but nothing seems to work!

(I have no problems catching key events during gameplay either from OnGui() with Event.current.* or from Update() with Input.GetKeyxxx())

Any clues how to do it?

Dan,

Then you have to use mouseHook and keyBoardHook in you code. You can find it .net forum. Just google it. i done it before but its not handy.

Hi UnityCoder, thanks for taking the time with a hint to set me up in the right direction… could have looked a long time!

Googling for this, I found a C# resource here and a VB.net technique here

Do you think these solutions would work in Mono / Unity?

Thanks again! :slight_smile:

Hi all, researching more into this, both the C# and VB links above fail because I can’t register the keyboard hook with window.

Module[ ] list = System.Reflection.Assembly.GetExecutingAssembly().GetModules();
IntPtr hInstance = Marshal.GetHINSTANCE(list[0]);
_hookHandle = SetWindowsHookEx(_hookType, _hookFunction, hInstance, 0);

SetWindowsHookEx fails probably because the GetHINSTANCE returns 0xFFFFFFFF. list[0] appears a legit module info.

Any idea how to obtain a valid hInstance from an assembly so I can see if SetWindowsHookEx can work?

Thanks!

Still could not make this work, but I found that Event.current.modifiers returns many useful keys such as Alt, Control, Shift, Caplock, etc. and was able to make do with that. :slight_smile:

Ofcourse its work in unity. I tried it in Unity. May be i can try to find that source tomorrow in office which i used and post it on this thread.

Hi UnityCoder,

Researching this some more, some of the classes derived from UnityEditor do get keyboard events for keys not ‘eaten’ by Unity from Event.current… it’s just that OnEditorGui() gets lots of events that makes a clean OnKeyDown() / OnKeyUp() implementation difficult.

So it looks I can make do with what I have thus far, but if it doesn’t take you too long, do post a solution and it may be better than what I can make run with Event.current… and I’m sure some future reader could benefit as well!

Thanks again for the offer! :):):slight_smile:

Hi all, if this helps anyone, the code below is able to obtain what character is pressed. Class must extend from ‘Editor’ and be in an ‘Editor’ folder!

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(CMyObject))]
public class CMyObjectEd : Editor {

void OnSceneGUI() {
Event e = Event.current;
if (e.type == EventType.KeyDown) {
Debug.Log("Ev.KeyDown: " + e);
}
}
}

I tried with
SetWindowsHookEx and didn’t succeed. So instead to hook to WndProc of Unity Window I create new window and wire delegate to its WndProc and listen to WM_KEY(Suppose not just focused wndow get key event). See WinHIDInterface.cs https://github.com/winalex/Unity3d-InputMapper. You can also query HID for Keyboard/s, get device path and open/read so on…

No, no, no… Trying to hook yourself on HookEx is terrible idea and will not work on Mac or Linux.

What is your editor? An EditorWindow? An Editor?

In editor mode, there is no Input.GetKey, so you must rely on Event.current.type == EventType.MouseDown.
It works fine, I’ve use it countless time.

Its a library for inputs overall(keys,mouse,joysticks). There is an editor too they can allow you easy map inputs to states. It using own system for joysticks outside unity and now work in web player too. At least with Chrome and FF(not tested .
https://www.youtube.com/watch?v=kX0j69ZgeCw