控股箭头键可朝人物移动C#.NET问题 [英] Holding Arrow Keys Down For Character Movement C# .Net ISSUES
问题描述
因此,在短期我只是想走动Canvas对象矩形在WPF应用程序。我在这里是我的KeyDown事件的功能。问题是,当我抓住重点下来时间不长,它在启动该功能,再次快速螺丝了我的矩形位置code。
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.
我的理论/其背后的逻辑: 因为当你举行一个按键时,键盘不会移动顺利(测试上的滚动条在浏览器中,它开始,暂停,然后继续顺畅),我希望它开始形成定时器,移动对象的UI。然后,当KeyUp事件发生时,计时器停止。
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();
}
}
}
和仅供参考,如果你需要我的矩形的类code我会后,如果右箭头键是pressed会发生什么:
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被称为:
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();
}
}
}
最后,StopIt是所谓的KeyUp事件:
Finally, StopIt is called in the KeyUp event:
public void StopIt()
{
tmr.Stop();
goRight = true;
goLeft = true;
goUp = true;
goDown = true;
}
我只学习C#了几个月,所以我现在正在努力保持它如果可能的话比较简单,而且只使用.NET。
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.
任何帮助将是AP preciated !!
Any help would be appreciated!!
: 我的解决方法:
我只是做我的左右开关壳体上,而(标志)循环。然后,我把标志=中的情况下,假的。当键上为pressed我重新设置标志等于true。 YAY
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
推荐答案
我假设你希望你的角色移动初始KeyDown事件。然后,你想,直到你得到一个KeyUp事件忽略任何后续的KeyDown事件。
I assume that you want your character to move on the initial KeyDown event. Then you want to ignore any subsequent KeyDown events until you get a KeyUp event.
所以,你可以通过检查忽略后续的KeyDown事件e.IsRepeat例如
So you can ignore the subsequent KeyDown events by checking e.IsRepeat e.g.
public void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.IsRepeat) return;
// rest of your code...
顺便说一句,你滚动的应用程序时遵循非平滑移动的键盘重复延迟造成的。您可以设置这在键盘属性或虽然的http://msdn.microsoft.com/en-us/library/system.windows.systemparameters.keyboarddelay.aspx
这篇关于控股箭头键可朝人物移动C#.NET问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!