Search Unity

current event not detecting shift key

Discussion in 'Editor & General Support' started by AzulValium, Sep 14, 2009.

  1. AzulValium

    AzulValium

    Joined:
    Sep 14, 2009
    Posts:
    103
    well, I believe the subject says everything.

    Event.current.keyCode returns 0, but current.shift returns true.

    It happens on editor and win stand-alone.

    Code (csharp):
    1.  
    2. void OnGUI() {
    3.         Debug.Log("Event.current.keyCode: " + Event.current.keyCode);
    4.         Debug.Log("Event.current.shift: " + Event.current.shift);
    5. }
    6.  
    Sorry for my english !!!!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I think you've found a genuine issue here - I've reported it.
     
  3. AzulValium

    AzulValium

    Joined:
    Sep 14, 2009
    Posts:
    103
    thanks, but are you sure? perhaps is just some windows configuration.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I tried it on a Mac and it didn't work there either ;-)

    In any case, the documentation should probably warn you that this problem can happen.
     
  5. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,494
    This is still happening in 3.4
     
  6. sh0v0r

    sh0v0r

    Joined:
    Nov 29, 2010
    Posts:
    285
    I just ran into this issue as well in version 4.0... anyone got an idea for a solution?
     
  7. sdgd

    sdgd

    Joined:
    Jan 15, 2013
    Posts:
    81
    still same on 4.0.1

    but I've found a Partionally solution if anyone is interested I'm kinda playing with this about 2 days

    Code (csharp):
    1.  
    2.     string asd;
    3.     Event e = Event.current;
    4.     if (e.shift){
    5.         asd = "L/R Shift";
    6.     }
    7.  
    my full getting keys code is:
    Code (csharp):
    1.  
    2.     public string asd;
    3.     public string delta;
    4.     public int asdf = 0;
    5.     public int rev = 0;
    6.     void OnGUI(){
    7.        
    8.        
    9.        
    10.        
    11.  
    12.         Event e = Event.current;
    13.        
    14.             if (e.shift){
    15.                 Debug.Log("Pressed: " + e.commandName);
    16.             }
    17.         if (e.delta.ToString() == "(0.0, -3.0)"){
    18.             rev++;
    19.             if (rev > 1){asdf--; rev=0;}
    20.             asdf++;
    21.         }
    22.        
    23.         if (e.delta.ToString() == "(0.0, 3.0)"){
    24.            
    25.             rev--;
    26.             if (rev < -1){asdf++; rev=0;}
    27.             asdf--;
    28.         }
    29.        
    30.        
    31.        
    32.        
    33.         if (e.delta.ToString() != "(0.0, 0.0)"){
    34.             delta = e.delta.ToString();
    35.         }
    36.        
    37.        
    38.        
    39.         if (e.functionKey || e.isKey || e.isMouse || e.command || e.shift){
    40.             if (e.keyCode.ToString() != "None"){
    41.                 asd = e.keyCode.ToString();
    42.             }
    43.             else if (e.shift){
    44.                 asd = "L/R Shift";
    45.             }
    46.         }
    47. }
    48.  
    49.  
    50.  
    about mouse wheel I'll probably have to tweak it a bit so I won't need to make reverse divide by 2



    and in inspector you can clearly see how are key pressed

    don't know if I'll still find this post to repair it when I improve my key fetching

    am about shift I figured out with first statement up there he gave me idea how to fetch shift
     
    Last edited: Feb 11, 2013
  8. sdgd

    sdgd

    Joined:
    Jan 15, 2013
    Posts:
    81
    Got it all working as a charm

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public struct KeyBindings {
    6.     public string Keys;
    7.     public bool DontTurnOffToggle;
    8.    
    9.     public void UseSpecialKeysF (){
    10.        
    11.     }
    12.    
    13.     public void CatchAllKeysF () {
    14.         DontTurnOffToggle = true;
    15.        
    16.         // input needs every code to be found and set to string while event finds it him self
    17.         Event e = Event.current;
    18.        
    19.         if (Input.anyKey || e.functionKey || (e.type.ToString() == "scrollWheel")){ // SysReq (Print Screen)  scrollWheel don't work on any key
    20.            
    21.             // this keys don't work on Event.current so we do imputs first
    22.             // we catch all mouse buttons (even if event has first 3 mouse buttons)
    23.             for (int i=0; i < 6  (Keys != "/") ; i++) {
    24.                 if (Input.GetMouseButton(i)){
    25.                     Keys = "Mouse Button " + i;
    26.                     DontTurnOffToggle = false;
    27.                 }
    28.             }
    29.            
    30.             if (e.shift){// we check with Event if shift is down
    31.                 if (Input.GetKey(KeyCode.LeftShift)){   // we check with input witch shift is down
    32.                     Keys = "Left Shift";
    33.                     DontTurnOffToggle = false;
    34.                 }
    35.                 else if (Input.GetKey(KeyCode.RightShift)){// we check with input witch shift is down
    36.                     Keys = "Right Shift";
    37.                     DontTurnOffToggle = false;
    38.                 }
    39.             }
    40.            
    41.             // we must catch 2 input keys ž  < as otherwise both will become backslash
    42.             else if (Input.GetKey(KeyCode.Backslash)){
    43.                 Keys = "/";
    44.             }
    45.            
    46.             // Events starts
    47.             // we catch all Keyboard keys
    48.             else if (e.keyCode.ToString() != "None"  (! (Input.GetKey(KeyCode.Backslash) ||
    49.                                                                 Input.GetKey(KeyCode.Return) )  )  ){// (! Input.GetKey(KeyCode.Backslash)) is here if they fix this it works even without this witch shouldn't
    50.                 Keys = e.keyCode.ToString();
    51.                 DontTurnOffToggle = false;
    52.             }
    53.            
    54.             // we catch MouseWheel Forward/Backward
    55.             else if (e.delta.ToString() == "(0.0, -3.0)"){
    56.                 Keys = "scroll Wheel Forward";
    57.                 DontTurnOffToggle = false;
    58.             }
    59.             else if (e.delta.ToString() == "(0.0, 3.0)"){
    60.                 Keys = "scroll Wheel Backward";
    61.                 DontTurnOffToggle = false;
    62.             }
    63.             // OVERRIDER
    64.             if (Keys == "/"  Input.GetKey(KeyCode.Return)){
    65.                 DontTurnOffToggle = false;
    66.             }
    67.             if (Keys != "/"  Input.GetKey(KeyCode.Return)){
    68.                 Keys = e.keyCode.ToString();
    69.                 DontTurnOffToggle = false;
    70.             }
    71.         }
    72.     }
    73. }
    74.  
    75.  
    76.  
    77.  
    78.  
    79.  
    80. /* only notes left not everything works out of box some things need to get some public variables to make them work
    81.  *                                                                  (espacelly to see them working in inspector)
    82.         //learning type of events
    83.        
    84.         catching all keys:
    85.        
    86.        
    87.        
    88.        
    89.         // event way (doesn't get Mouse keys 3+)
    90.         if ((e.type.ToString() == "KeyDown") || (e.type.ToString() == "mouseDown") || (e.type.ToString() == "scrollWheel")
    91.                 || e.shift || e.functionKey){} // Shift  SysReq (Print Screen) don't work on "KeyDown"
    92.        
    93.        
    94.         // input way
    95.         if (Input.anyKey || e.functionKey || (e.type.ToString() == "scrollWheel")){} // SysReq (Print Screen)  scrollWheel don't work on any key
    96.        
    97.        
    98.         if ((e.type.ToString() != "Repaint")  (e.type.ToString() != "Layout")){
    99.             Debug.Log("Pressed: " + e.type);
    100.         }
    101. //       * Event.current.Type
    102. //       * KeyDown      is doing continuously
    103. //       * KeyUp        is doing 2 times at releace
    104. //       * mouseDown    only once
    105. //       * mouseUp      only once
    106. //       * mouseDrag    when mouse is down and you are draging it it's updated only for each dragging
    107. //                  *   it's stopped when no dragging even if mouse down
    108. //       * scrollWheel  on mouse wheel
    109. //       *
    110. //       * used         when I press a button
    111.        
    112.         //catching mouse buttons 1-3
    113.         if (e.isMouse){
    114.             Keys = e.button.ToString();
    115.         }
    116.        
    117.         //catching mousewheel forward // is calles on begin and end so I did a / 2 with ++/--rev
    118.         if (e.delta.ToString() == "(0.0, -3.0)"){
    119.             rev++;
    120.             if (rev > 1){Keysf--; rev=0;}
    121.             Keysf++;
    122.         }
    123.         //catching mousewheel backward
    124.         if (e.delta.ToString() == "(0.0, 3.0)"){
    125.            
    126.             rev--;
    127.             if (rev < -1){Keysf++; rev=0;}
    128.             Keysf--;
    129.         }
    130.        
    131.        
    132.        
    133.         // figureiing out delta
    134.         if (e.delta.ToString() != "(0.0, 0.0)"){
    135.             delta = e.delta.ToString();
    136.         }
    137.        
    138.        
    139.         // catching all keyboard controlls
    140.         if (e.functionKey || e.isKey || e.isMouse || e.command || e.shift){
    141.             if (e.keyCode.ToString() != "None"){
    142.                 Keys = e.keyCode.ToString();
    143.             }
    144.             else if (e.shift){
    145.                 Keys = "L/R Shift";
    146.             }
    147.         }
    148. */
    149.  
    if anyone knows how to find character next to Y I'd appreciate

    I get with event both backslash < and ž

    < is bottom left next to Y

    and ž is in middle back of Return

    and they are both backslash
     
    Last edited: Feb 11, 2013
  9. nathanguttridge

    nathanguttridge

    Joined:
    Apr 5, 2013
    Posts:
    2
    Hi,

    I am having a slight problem with this code...
    If I am holding down a direction key and then press shift it works but doesn't detect that I have released shift until I have released the direction key too.
    Are you using the DontTurnOffToggle somehow to fix this?


    Cheers
     
  10. Izitmee

    Izitmee

    Joined:
    Jan 27, 2011
    Posts:
    3,227
    Still doesn't work on Unity 4.1.5, and the manual still has no warning :/
     
  11. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    I reported a bug for shift keys in April this year, here is the thread :

    http://forum.unity3d.com/threads/178942-Unity-3-5-7f6-dual-shift-key-bug

    tested with "GetKey" , "GetKeyUp" and "GetKeyDown" - code included in thread - confirmed by another user - sample package sent with bug report - nothing happened

    case number : 538538

    I received a mail with a link to fogBugs to trace the bug report ..

    FogBugs link : https://fogbugz.unity3d.com/default....qm77l94pqksfhg

    If you don't want to go check the thread above then in a nut shell it describes the bug as been :

    If you press and hold one shift key and then another shift key - if you release the second pressed shift key first, it will not fire and event. In fact it becomes completely unresponsive from that point onwards, so you can press and release it all you want thereafter, nothing will fire until you release the first pressed shift key.

    This is not a keyboard hardware problem (eg non n-key rollover etc) as I have tested it in other software running on my machine and there is no problem in handling dual shift key events
     
    Last edited: Jun 18, 2013
  12. Izitmee

    Izitmee

    Joined:
    Jan 27, 2011
    Posts:
    3,227
    Agh!!! SHIFT is such an important key in games! Give us bugs with... I don't know... TAB! But not with SHIFT! :/
     
  13. yoyo

    yoyo

    Joined:
    Apr 16, 2010
    Posts:
    112
    Still happening in Unity 4.2 :p

    I implemented my key up/down handling in separate methods. Then I poll for shift keys when Event.current.type == EventType.Repaint, keep track of current shift key state, and call my key handling methods when the state changes. Kludgy, but at least it lets me handle all my key events in one place.
     
  14. Izitmee

    Izitmee

    Joined:
    Jan 27, 2011
    Posts:
    3,227
    I did the same, but it's a pain in the ass and my code looks so unpolished like that :(
     
  15. asafsitner

    asafsitner

    Joined:
    Mar 3, 2012
    Posts:
    72
    I can confirm this happens in Unity 4.3.1f1 :(

    What I did to identify shift (and any other modifier key along the way, which was a nice bonus) was this:

    Code (csharp):
    1. if(Event.current.modifiers > 0)
    2.   {
    3.    if((Event.current.modifiers  EventModifiers.Shift) == EventModifiers.Shift)
    4.    {
    5.     Debug.Log("shift");
    6.    }
    7.  
    8.    if((Event.current.modifiers  EventModifiers.Control) == EventModifiers.Control)
    9.    {
    10.     Debug.Log("control");
    11.    }
    12.  
    13. //etc...
    14.   }
    It's also possible to work with many simultaneous modifiers this way.
    Hope this helps anyone who comes across!
     
  16. SantaKlaus

    SantaKlaus

    Joined:
    Mar 3, 2013
    Posts:
    10
    Hello all,

    It d be great if someone from Unity could comment on the status of this issue, if it is a feature or a bug, or maybe on how Shift-Key is supposed (not) to be used according to Unity-Philosophy.

    Best regards,
     
  17. Izitmee

    Izitmee

    Joined:
    Jan 27, 2011
    Posts:
    3,227
    Yup, would like an official response too, or even better a fix. The link to the bug says it was "moved", and the other thread has no answer as this one.
     
  18. Izitmee

    Izitmee

    Joined:
    Jan 27, 2011
    Posts:
    3,227
    Necro bump, but I have something to add.

    In Unity 5.3.1 I'm trying to detect the exact moment a SHIFT key is pressed inside an EditorWindow (so not in runtime). Apparently, KeyDown events are not fired when SHIFT is pressed at all, while they work with ALT and CONTROL for example. Any news about this?
     
  19. Sleights

    Sleights

    Joined:
    Dec 26, 2014
    Posts:
    2
    Still an issue in 5.2.4

    Here is my workaround that seems to function well enough.

    Code (CSharp):
    1.  
    2.         if( e.shift )
    3.         {
    4.             KeyCode lShift = KeyCode.LeftShift;
    5.             KeyCode rShift = KeyCode.RightShift;
    6.             KeyCode thisShift = KeyCode.None;
    7.  
    8.             if( Input.GetKey(lShift) )
    9.             {
    10.                 thisShift = lShift;
    11.             }
    12.             else if( Input.GetKey(rShift) )
    13.             {
    14.                 thisShift = rShift;
    15.             }
    16.  
     
  20. Sleights

    Sleights

    Joined:
    Dec 26, 2014
    Posts:
    2
    This actually only works in Editor, in a build, we don't even get any events to check to see if shift is held down.
     
  21. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    I just had to add special code to handle this myself. Is shift the only key with this issue?
     
  22. oxysofts

    oxysofts

    Joined:
    Dec 17, 2015
    Posts:
    12
    One year later, 2017, still not fixed
     
  23. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    119
    I have no idea why the normal isKey event can't detect shift keys when it works for everything else.

    I also have no idea why the shift event doesn't function outside of the editor. If anyone knows why please do elaborate.


    I mean sure in my case I can detect it using Input.GetKeyDown in the Update() bunch, but then my code is split between OnUpdate() and OnGUI() for no apparent reason.
     
  24. Doomlaser

    Doomlaser

    Joined:
    Dec 9, 2013
    Posts:
    6
    Well, this has bitten me. Reading this tread is incredibly frustrating. This seems like a pretty fundamental input flaw to have someone fix after 9 years.

    Still broken in 2017.3.0f3 it appears.
     
  25. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    99
    Yep, just spent ages looking into this.

    How can this not have been fixed (or commented on by Unity staff) in over 9 years... total waste of my time :(
     
  26. ceebeee

    ceebeee

    Joined:
    Mar 7, 2017
    Posts:
    377
    Bump. It's kind of crazy there's no way to detect when the shift key is pressed. It's such a basic thing...
     
  27. papirosnik

    papirosnik

    Joined:
    Feb 17, 2012
    Posts:
    5
    2018.2.b9
    Shift still can't be detected... (
     
    tgaldi likes this.
  28. Owen-LBBH

    Owen-LBBH

    Joined:
    Aug 2, 2017
    Posts:
    1
    2019.1.0a10 still happening...
     
    polyflow3d likes this.
  29. exonicus

    exonicus

    Joined:
    Dec 13, 2016
    Posts:
    1
    Hi I try example script from help
    https://docs.unity3d.com/ScriptReference/Event-character.html
    Code (CSharp):
    1.  Event e = Event.current;
    2.         if (e.isKey)
    3.         {
    4.             Debug.Log("Detected character: " + e.character);
    5.         }
    And get empty character only, but keyCode is showing pressed key
    Unity 2018.3f14
    Unity 2019.3.0a6