Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by simplicitygs · Jun 04, 2014 at 11:30 PM · 0

Shift key down? help

I am beyond confused. I have already searched through the ask questions and googled this problem, I can't seem to get it to detect the shift key. The current script I am using is if (Input.GetKeyDown("shift")) { Debug.Log("shift key was pressed"); }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by rutter · Jun 04, 2014 at 11:31 PM

Unity recognizes "left shift" and "right shift", but not "shift".

Use something like this:

 Input.GetKeyDown("left shift")

Or this:

 Input.GetKeyDown(KeyCode.LeftShift)

To respond to either key, you can check both of them:

 Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)

You can find a full list of supported keys at the input manual page or KeyCode enum page.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image simplicitygs · Jun 04, 2014 at 11:42 PM 0
Share

Am I doing something completely wrong than, I tried all 3 ways for this code and it's not showing test on the debug log D: if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift)) { Debug.Log("test"); }

avatar image Kiwasi · Jun 05, 2014 at 12:04 AM 0
Share

Where are you putting your code? It belongs in update.

avatar image ruudvangaal · Nov 09, 2020 at 08:05 AM 0
Share

You often want GetKey(), which stays 'true' as long as the key is depressed. GetKeyDown() only becomes true in the one frame you start pressing it. For example, I use it to tweak scroll wheel behavior, so:

if(Input.GetKey(KeyCode.LeftShift)) modify_size; else modify_position;

avatar image ruudvangaal · Apr 15 at 02:00 PM 0
Share

The Input.* method works in Update(). If you're in a custom inspector or EditorWindow, you'll want to use event.shift instead, which contains the state of the SHIFT key as it happened at the time the event took place (which is better than the current state of the SHIFT key really).

avatar image
4

Answer by jeff-smith · Jun 20, 2016 at 04:58 PM

In your update method, you can do this:

 void Update()
 {
     bool isShiftKeyDown = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Free286 · Jan 08, 2018 at 12:30 AM

You need to use GetKey() instead of GetKeyDown(). The first one will return true if the shift key is down. The second one will return true only the moment the shift key is pressed (one frame).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by idlebyte · Nov 22, 2014 at 02:39 PM

Check: http://answers.unity3d.com/answers/838436/view.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by awesomedata · Feb 06, 2018 at 04:37 AM

There is no internal association with the Left/Right Shift keycode.


This is a confirmed bug by Unity. It has been in Unity since forever (pre-2010), but I reported it myself about two weeks ago.


It's Impossible to check Event.current.keyCode using the "KeyCode.LeftShift" and "KeyCode.RightShift" representations of the object Enum. The Enum object exists, but the numerical value it was supposed to associate with "LeftShift" and "RightShift" objects (written in the Documenation) does not actually exist. The LeftControl and LeftAlt variations do work. It is only LeftShift/RightShift that doesn't associate with anything that can be tested in "Event.current.keyCode".

How you can reproduce it:

Easy -- Just make a new project, make a new C# script called "TestKeycodes" then place it in an "Editor" folder, and finally, simply paste the following code into that new Editor Script:

 using UnityEngine;
 using UnityEditor;
 
 [InitializeOnLoad]
 [ExecuteInEditMode]
 public class TestKeycodes : EditorWindow
 {
 
     [InitializeOnLoadMethod]
     public static void InitTest()
     {
         SceneView.onSceneGUIDelegate -= OnGUI;
         SceneView.onSceneGUIDelegate += OnGUI;
     }
 
 static void OnGUI()
 {
 Debug.Log(Event.current.keyCode);
 }
 
 }

Make sure your class runs in Edit Mode constantly and automatically!

Now run the script and watch the Console.

It will display "None" constantly in the console. Press LeftControl or RightAlt or the "A" or "P" keys. You should see them registering in the Console. Now Press LeftShift and RightShift however many times you want. You will see nothing about them appearing in the console no matter when or how you press them. This is because they don't exist.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Doomlaser · Mar 21, 2018 at 04:49 PM 0
Share

Seriously? This has just bit me. Is there any workaround? This has cost me time.

avatar image awesomedata Doomlaser · Mar 21, 2018 at 11:46 PM 0
Share

Yep, I reported the bug several months ago -- I don't suspect it's fixed yet either.

Anyway, I think the closest I've gotten to this is a manual "Event.current.shift" check in an OnGUI event that fires when the enum of a player's custom key config is set to $$anonymous$$eyCode.LeftShift's value as an "int" (I think its int value is 310 or something? I'm probably wrong, but it's somewhere like that.) But yeah, then you'll have to use OnGUI to check the result from that Event.current.shift and manually send it to the game loop if you want to legitimately grab a player's input. You can just increase an int every game step while Event.current.shift is pressed and if it's == 1, it's the initial keypress, if it's 0 then it's not pressed, and if it's > 0 then it's been pressed for more than 1 frame.



Hope that helps!

avatar image Bunny83 · Mar 22, 2018 at 01:40 AM 0
Share

While it is true that OnGUI won't receive shift key down / up events it was never meant for this. The shift key is a modifer key and it's state can be queried with Event.current.shift. Apart from that using Input.GetKeyDown(KeyCode.LeftShift) inside Update works fine. So the KeyCode for LeftShift and RightShift is a valid keycode. It has an actual value and it works. It's just that OnGUI doesn't receive keydown / up events for those keys.


I'm not sure why they do not propergate the shift key down / up events in OnGUI but as i said it was never meant to be used as general input method. Unfortunately Unity doesn't provide a way to manually queue new events. If you want to use OnGUI to process key events you can however create a seperate method that processes events and manually send those events. Something like this:

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.LeftShift))
         ProcessEvent(new Event(0) { type = EventType.KeyDown, keyCode = KeyCode.LeftShift, shift = true });
     if (Input.GetKeyDown(KeyCode.RightShift))
         ProcessEvent(new Event(0) { type = EventType.KeyDown, keyCode = KeyCode.RightShift, shift = true });
     if (Input.GetKeyUp(KeyCode.LeftShift))
         ProcessEvent(new Event(0) { type = EventType.KeyUp, keyCode = KeyCode.LeftShift });
     if (Input.GetKeyUp(KeyCode.RightShift))
         ProcessEvent(new Event(0) { type = EventType.KeyUp, keyCode = KeyCode.RightShift });
 }
 
 void OnGUI()
 {
     Event e = Event.current;
     ProcessEvent(e);
 }
 void ProcessEvent(Event e)
 {
     if (e.isKey)
     {
         Debug.Log("Event: " +e.type + " Key: " + e.keyCode + " shiftState: " + e.shift);
     }
 }

The ProcessEvent method will get all events you would get in OnGUI but in addition it gets the left and right shift down and up events. However we only get those events once when the key is pressed down. That means, unlike the control key for example, the key down event is not repeated with the keyboard repeat rate. Also it should be obvious that you can not use GUI methods inside "ProcessEvent". GUI functions only work when they are called from an actual GUI callback. Unity has an internal flag that signals this. This flag will even make Event.current to return null when used outside a GUI callback. So this is only a solution if you just want to process key events like that.

avatar image awesomedata Bunny83 · Mar 26, 2018 at 02:07 PM 0
Share

OnGUI won't receive shift key down / up events -- it was never meant for this. The shift key is a modifer key and it's state can be queried with Event.current.shift.

This is not true. Although hardware can be different, keycodes are meant to come across no matter what context they are used. To prove my point, use $$anonymous$$eyCode.LeftShift in any "Input" method call, and just like in OnGUI (at least when I tested it last), it still refuses to work. Additionally, you don't have the option of using Event.current.shift to check both at once. -- In contrast, $$anonymous$$eyCode.Left/RightControl and $$anonymous$$eyCode.Left/RightAlt work fine -- in BOTH "Event" and "Input" contexts -- and also do keyup/keydown events too.

Because a general-purpose keycode like $$anonymous$$eyCode.Shift doesn't exist to perform the equivalent function of the Event.current.shift option from OnGUI in the "Input" methods, it's clear Unity intends you to use the Left/Right versions of the keycode independently (and/or simultaneously, if you needed it) -- in all cases.



Additionally, even after writing 14+ lines of code to do this with the exact code you did it, I was still not able to get that code to generate input's $$anonymous$$eyDown and $$anonymous$$eyUp events using those two $$anonymous$$eyCodes. I have submitted the bug report some months back on this and Unity reproduced it -- but I'm not sure whether it will be fixed in the near future or not.

avatar image Bunny83 awesomedata · Mar 26, 2018 at 05:19 PM 0
Share

use KeyCode.LeftShift in any "Input" method call [ ... ] it still refuses to work

The shift keycode has always worked for me in any Input.GetKey / Down / Up method since Unity version 2.6 and it's still working.


And no, keycodes are not necessarily be meant to be valid in all contexts. This even applies to windows API callbacks and windows messages. Windows has it's own virtual key code list and not every key has an actual constant (like the number keys(0-9) and letter characters (a-z,A-Z) for example). Though they may still be present. Likewise depending on the callback certain virtual keycodes might not be returned by certain callbacks. OnGUI is a gui callback is was never meant to process gameinput. It's meant to process keyboard input for the GUI controls.


Don't get me wrong, I did not say that i'm happy with the fact that the shift key down / up events are not send through the GUI event queue. I have just explained the current behaviour and the fact that it is nowhere documented which keys are actually processed through the legacy gui system.


Also note that virtual keycodes are mapped to actual scancodes which are generated by the actual hardware. Depending on the hardware, the used driver and the used keyboard layout it's never guaranteed to get the same result on different devices.


I've tested the code i've posted in my answer and it works fine on my Win10 installation.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add SFX to thia script 0 Answers

Make a code line execute only once 2 Answers

Get which objects are changed during an undo/redo process 1 Answer

Naming a gui? 1 Answer

for not unity 3D for mobile? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges