Keys and Disabling
-
hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :
private void Form1\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { //tim\_Player.Enabled = true; Player.Direction = 1; } if (e.KeyCode == Keys.Right) { //tim\_Player.Enabled = true; Player.Direction = 3; } if (e.KeyCode == Keys.Down) { //tim\_Player.Enabled = true; Player.Direction = 2; } if (e.KeyCode == Keys.Left) { //tim\_Player.Enabled = true; Player.Direction = 4; } }
I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P
-
hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :
private void Form1\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { //tim\_Player.Enabled = true; Player.Direction = 1; } if (e.KeyCode == Keys.Right) { //tim\_Player.Enabled = true; Player.Direction = 3; } if (e.KeyCode == Keys.Down) { //tim\_Player.Enabled = true; Player.Direction = 2; } if (e.KeyCode == Keys.Left) { //tim\_Player.Enabled = true; Player.Direction = 4; } }
I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P
-
hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :
private void Form1\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { //tim\_Player.Enabled = true; Player.Direction = 1; } if (e.KeyCode == Keys.Right) { //tim\_Player.Enabled = true; Player.Direction = 3; } if (e.KeyCode == Keys.Down) { //tim\_Player.Enabled = true; Player.Direction = 2; } if (e.KeyCode == Keys.Left) { //tim\_Player.Enabled = true; Player.Direction = 4; } }
I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P
-
hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :
private void Form1\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { //tim\_Player.Enabled = true; Player.Direction = 1; } if (e.KeyCode == Keys.Right) { //tim\_Player.Enabled = true; Player.Direction = 3; } if (e.KeyCode == Keys.Down) { //tim\_Player.Enabled = true; Player.Direction = 2; } if (e.KeyCode == Keys.Left) { //tim\_Player.Enabled = true; Player.Direction = 4; } }
I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P
Don't mean to step on the other guys who answered, but it looks like you're trying to have Up and Down cancel each other out (And Left+Right)... This'll take a little more than what you have now, but not very much. The easiest way would probably be to just keep track of which keys are currently pressed. Now, you can use API calls to query them directly, but if you want to stick to simple events, here's one way to do it: 1) Create a dictionary mapping keys to a boolean 2) Whenever a key you're watching is pressed OR released (Handle the KeyUp event too!), set its value in that dictionary to true (Pressed) or false (Not pressed)... If you want to make this more readable and intuitive, you can even use an enum instead of a boolean. 3) Whenever you make a change to the dictionary, run a separate function to test the current key states... Then you can check for things like
if (IsKeyPressed[Keys.Left] && !IsKeyPressed[Keys.Right])
... There are plenty of other ways to do this, but this is one of the simplest... If you want to be more efficient, you can use a single flags-type enum value instead of a dictionary, and use binary operations to set and unset the various components:[Flags]
enum ArrowKeyStates {
None = 0,Left = 1,
Up = 2,
Right = 4,
Down = 8,// Then a few more to make testing for diagonals easy...
UpLeft = 3,
UpRight = 6,
DownLeft = 9,
DownRight = 12
}// To set a key as pressed, use a binary OR:
MyKeyState |= ArrowKeyStates.Left;// To set a key as released, use a binary XOR:
MyKeyState ^= ArrowKeyStates.Left;Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :
private void Form1\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { //tim\_Player.Enabled = true; Player.Direction = 1; } if (e.KeyCode == Keys.Right) { //tim\_Player.Enabled = true; Player.Direction = 3; } if (e.KeyCode == Keys.Down) { //tim\_Player.Enabled = true; Player.Direction = 2; } if (e.KeyCode == Keys.Left) { //tim\_Player.Enabled = true; Player.Direction = 4; } }
I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled ..... Thanks in advance :P