예제 #1
0
 public override void HandleInput(Input input)
 {
     if (input.IsKeyDown(Keys.Q))
     {
         move(8);
     }
 }
예제 #2
0
        public void HandleKeyInput(Input.Input input)
        {
            Keys[] pressed = input.GetPressedKeys();

            if (selected && editable)
            {
                if (pressed.Length > 0) // dont do anything if keys have not been pressed
                {
                    for (int i = 0; i < pressed.Length; i++)
                    {
                        if (input.GetKeyState(pressed[i]) == InputState.Pressed)
                        {
                            if (KeyConverter.IsTypeableKey(pressed[i])) // no unicode characters
                            {
                                bool shift = input.IsKeyDown(Keys.RightShift) || input.IsKeyDown(Keys.LeftShift);
                                int asciiKey = KeyConverter.ConvertToAscii(pressed[i],shift);

                                if (!shift && asciiKey >= 65 && asciiKey <= 90)
                                {
                                    text = StringManip.AddCharAtPos(text, (char)(asciiKey + 32), textPos); // lower case
                                }
                                else
                                {
                                    text = StringManip.AddCharAtPos(text, (char)asciiKey, textPos);
                                }
                                textPos++;
                            }
                            else if (pressed[i] == Keys.Back) // explicitly handle backspace
                            {
                                if (text.Length > 0)
                                {
                                    text = StringManip.RemoveCharAtPos(text, textPos);
                                    textPos--;
                                    if (textPos < 0)
                                    {
                                        textPos = 0;
                                    }
                                }
                            }
                            else if (pressed[i] == Keys.Delete)
                            {
                                if (text.Length > 0 && textPos != text.Length)
                                {
                                    text = StringManip.RemoveCharAtPos(text, textPos + 1);
                                    if (textPos > text.Length)
                                    {
                                        textPos = text.Length;
                                    }
                                }
                            }
                            else if (pressed[i] == Keys.Enter) // explicitly handle enter
                            {
                                if (Enter != null)
                                {
                                    Enter(null, new EventArgs());
                                }
                                else
                                {
                                    text += '\n';
                                }
                            }
                            else if (pressed[i] == Keys.Left)
                            {
                                textPos--;
                                if (textPos < 0)
                                {
                                    textPos = 0;
                                }
                            }
                            else if (pressed[i] == Keys.Right)
                            {
                                textPos++;
                                if (textPos > text.Length)
                                {
                                    textPos = text.Length;
                                }
                            }
                        }
                    }
                }
            }
        }