Game
Kordex - The Golden Path
6 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

Just as Pecaminosa's musicians #improvise this rendition of #LittleSunflower 🌻, so do we improvise with this post you're reading. 😂

What's coming out of your own improvs? 🤔

#Pecaminosa | #IndieDev | #OST | #Jazz

Did you know that today is #InternationalJazzDay? 🎷😎

No? 😯 Well, we couldn't pass up this date!

And what better to celebrate this day than #Pecaminosa #OST? 🎷

Let us know what you think about it and have a great weekend!

art comission.

Been working lately on lots of 'behind-the-scenes' boring stuff that no one really cares about, so here’s a guy playing the sax for some reason.

#screenshotsaturday

🍒Did you like the game? A demo is available for free on Steam.

✌️ Thanks for playing IndieGameiacs!

⚙️ Free Demo: http://bit.ly/UniDuni

📺 Full Video: https://youtu.be/5qEbVXg7GaQ

#steam #uniduni #clops #screenshotsaturday

Protege el conocimiento, salva la historia. Guardian of Lore es un platformer 2D en el que debes luchar para mantener viva la memoria de la mitología latinoamericana. El juego llegará a Steam el 18 de mayo: https://steam.pm/app/1211740 #ScreenshotSaturday

Family band complete!

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

Finished work on the emotions of a new character for the tank universe