Game
Kordex - The Golden Path
7 years ago

Log #9: How to detect key pressed events within the Unity Editor


Hey guys!

When writing your own editor scripts you’ll come to the point where you have to check whether or not a certain key (like Alt or Ctrl) is pressed. You may end up with the most obvious solution like the following:

5d09ad7761890.png

That’s cool, but it gets really unhandy if you need to add more buttons like the following:

5d09ad79efba0.png

Even worse is that for each editor script you have to repeat that process - this smells like bad code ^^

Static constructors are great!

I came up with the following solution: I’m creating an EventUtility-class containing a static Dictionary that stores a boolean for each possible KeyCode. Within a static constructor I initialize every possible KeyCode with false:

5d09ad7c10de1.png

Then I’m providing a static method which takes care about the key state and returns whether or not the key is pressed:

5d09ad7d7f678.png

Lazy Method

If you don’t want to prepare the dictionary in the static constructor beforehand, you can make IsKeyPressed(…) lazy:

5d09ad7f60eac.png

Other Problems

Sadly it’s not that easy because once the SceneView loses keyboard focus and regains it, it needs some frames before it detects, that the Alt-key is pressed and therefore returns false even though the user holds the key.

In one of our next posts, I’ll show you how to create a nice SceneViewEditorWindow which prepares some neat features for working with EditorWindows that uses SceneView-Features and provides a solution for the Focus-problem!

Thank you very much!



0 comments

Loading...

Next up

Log #2: New Island Part

This Satuday we invite to watch another #speedpainting of our artist Daniel Faiad.

How great is this scene of #Pecaminosa?

#ScreenShotSaturday | #IndieDev | #DigitalArt

Shoobies leave a sticky trail of mucus in their wake, which can impede movement for any creature that steps in it.

Finished a project I've been working on over the last few days!

Modeled and textured in Blender.

#3dart #lowpoly #npbr #fantasy #hut

Today I was setting up the location of the first boss. His name is "Father" and he is the first of the Patagonians. His task is to guard the road to the House.🛡🗡 Bookmark pre-launch page🔖🔖🔖

https://www.kickstarter.com/projects/rdvindiegame/the-patagonian…

So here's the first of the zodiac signs i will make for the next weeks.

A house I've built a while ago. :)

It nicely separates the snowy biome from the grassy one.

It's built with painted Ebonstone.

a drawing I did because I was bored😪. But I love how it turned out.😊

Regular exercise is key for healthy wings.

Try it out!

https://gamejolt.com/games/AeonNightmares/605184

Alone Together....