So in short i'm simply trying to move a rectangle around a Canvas object in a WPF application. What i have here is my KeyDown event function. The problem is, when i hold a key down for long, it launches this function over and over again rapidly and screws up my rectangle location code.
My theory/logic behind it: BECAUSE WHEN YOU HOLD A BUTTON DOWN ON A KEYBOARD IT DOES NOT MOVE SMOOTHLY (TEST IT ON THE SCROLL BAR IN YOUR BROWSER, IT STARTS, pauses, THEN CONTINUES SMOOTHLY), i want it to start a forms timer that moves the object in the UI. Then when the KeyUp event happens, the timer STOPS.
public void Window_KeyDown(object sender, KeyEventArgs e) { string msg; string keystr = e.Key.ToString(); Key keyval = e.Key; switch (keystr) { case "Down": Console.WriteLine("Case 1"); Display.Content = "Down"; foreach (Character character in creatures) { //character.buttondown = true; character.Position("Down"); } break; case "Up": Console.WriteLine("Case 2"); Display.Content = "Up"; foreach (Character character in creatures) { //character.buttondown = true; character.Position("Up"); } break; case "Left": Console.WriteLine("Case 3"); Display.Content = "Left"; foreach (Character character in creatures) { //character.buttondown = true; character.Position("Left"); } break; case "Right": Display.Content = "Right"; foreach (Character character in creatures) { //character.buttondown = true; character.Position("Right"); } break; } } public void Window_KeyUp(object sender, KeyEventArgs e) { Display.Content = "No key is pressed."; foreach (Character character in creatures) { if (e.Key == Key.Right) { character.StopIt(); } if (e.Key == Key.Left) { character.StopIt(); } if (e.Key == Key.Up) { character.StopIt(); } if (e.Key == Key.Down) { character.StopIt(); } } }
and just for reference if you need my rectangle class code i'll post what happens if the RIGHT arrow key is pressed:
Position is called
public void Position(String Direction) { if (Direction == "Right") { tmr = new System.Windows.Forms.Timer(); tmr.Interval = this.waitTime; tmr.Tick += new EventHandler(GoRight); tmr.Start(); } }
GoRight is called:
public void GoRight(object sender, System.EventArgs e) { if (x < Background.ActualWidth - CharacterWidth) { if (goRight) { x += incrementSize; CharacterImage.SetValue(Canvas.LeftProperty, x); } if (x > Background.ActualWidth - CharacterWidth) { goRight = false; tmr.Stop(); } } }
Finally, StopIt is called in the KeyUp event:
public void StopIt() { tmr.Stop(); goRight = true; goLeft = true; goUp = true; goDown = true; }
I've only been learning c# for a couple months now so i'm trying to keep it relatively simple if possible, and only use .net.
Any help would be appreciated!!
EDIT:: MY SOLUTION:
I simply made a while(flag) loop around my switch case. Then i set flag = false within the cases. When Key UP is pressed i set flag equal to true again. YAY