Checking for Return press AND shift?

Okay, simple question folks:

I’ve got a TextArea, that is supposed to send something when a button is hit (easy peasy) or shift+return/enter is pressed… now… I know that I gotta use the event.current features to get when a user is pressing return whilst in a textarea… and I have it like this:

Event e = Event.current;  
if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Return)
    {      
    	enterPressed = true;
    }

But if I change it within the {brackets} to this:

if(Input.GetKey(KeyCode.LeftShift))
	enterPressed = true;

it won’t get there…

so i believe I would have to use the event again, but there is no event that checks if the current key is held down, instead of being pressed to “create” the event itself…
help?

Hey there,

You don’t have to track the shift key, that would be kind of dumb if Unity missed out on that basic functionality; they are called EventModifiers

if(Event.current.keyCode == KeyCode.Return && Event.current.modifiers == EventModifiers.Shift)
{
  //Shift and return are pressed 
}

Cheers,

Well, it looks like you already have the right idea. Check when then key is pressed, then have a separate check for when the key is released.

bool shiftHeld = false;

// ...

if(e.type == EventType.KeyDown && e.keyCode == KeyCode.LeftShift)
{
    shiftHeld = true;
}
else if(e.type == EventType.KeyUp && e.keyCode == KeyCode.LeftShift)
{
    shiftHeld = false;
}

if(shiftHeld && e.type == EventType.KeyDown && e.keyCode == KeyCode.Return)
{
    // Enter was pressed while shift was held
}

There are certain key combinations that work in the build but not in the editor. For example, certain key combinations with the “Ctrl” modifier like Ctrl+Z won’t work if you’re testing inside the editor, but they will work when you build the game. I don’t think “Shift” falls in this category, but it might be worth checking.

Also, it might be helpful to print out the event information outside of the if statement:

Debug.Log(e.type + " " + e.keyCode);
if (e.type == /* ... */) { /* ... */ }

Also, there is this thing: Unity - Scripting API: Event.modifiers