Draw a handle in editor when key is pressed

Hello, how can i draw a handle in editor when certain key is pressed? I’am currently using this code in OnSceneGUI, but it’s not working.

if (Event.current.isKey && Event.current.keyCode == KeyCode.H)
{
    Handles.DotCap(0, new Vector3(), Quaternion.identity, 1);
}

Although, this is working fine

if (Event.current.shift)
{
    Handles.DotCap(0, new Vector3(), Quaternion.identity, 1);
}

Before drawing anything, you have to start the Handles context so that your Handles methods can be executed.

Handles.BeginGUI();
// Do your drawing stuff
Handles.EndGUI();

If you will have any problems to get access to your mouse events while the Editor script is enabled try to get the clicks like this:

void OnSceneGUI (){

	Event e = Event.current;
	int controlID = GUIUtility.GetControlID (FocusType.Passive);

	switch (e.GetTypeForControl (controlID)) {

	case EventType.MouseDown:
		GUIUtility.hotControl = controlID;
	
		CheckForPositions(e.mousePosition);
		e.Use ();

		break;

	case EventType.MouseUp:
		GUIUtility.hotControl = 0;
		e.Use();
		break;

	case EventType.MouseDrag:
		GUIUtility.hotControl = controlID;
		CheckForPositions(e.mousePosition);

		e.Use ();
		break;

	case EventType.KeyDown:
		if( e.keyCode == KeyCode.Escape ){
			// Do something on pressing Escape
		}

		if( e.keyCode == KeyCode.Space ){
			// Do something on pressing Spcae
		}

		if( e.keyCode == KeyCode.S ){
			// Do something on pressing S
		}

		break;
	}

	AllDrawingStuff();
}

And here the drawing

void AllDrawingStuff(){
	Handles.BeginGUI();

	DrawCircleOnMousePositions();
	DrawLine();

	Handles.EndGUI();
}

Here is a small tutorial which helped me to understand all the stuff!

Thanks to Bunny83’s comment I was able to figure out the proper way to do this.
You have to set a flag outside OnSceneGUI() and update the flag with the KeyDown & KeyUp event.
This would do something like this.

[CustomEditor(typeof(Test))]
public class TestInspector : Editor {
   private bool bDraw;

   void OnSceneGUI (){
        if (Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.H)
            bDraw= true;
        else if (Event.current.type == EventType.keyUp && Event.current.keyCode == KeyCode.H)
            bDraw= false;

        if (bDraw)
            Handles.CubeCap(0, Vector3.zero, Quaternion.identity, 1);
   }
}