如何使用键盘事件进行连续移动?
当前,此代码运行良好并移动picturebox,但它总是将其移动一次,等待大约一秒钟,然后继续移动它。如何使运动继续前进而不是停下来?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
if (pictureBox1.Location.X > 0)
{
pictureBox1.Location = new Point(x - 10, y);
}
}
}
Currently, this code runs fine, and moves the picturebox, but it always moves it once, waits about a second, and then continues to move it. How do I make the movement keep going instead of stopping for a second?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
if (pictureBox1.Location.X > 0)
{
pictureBox1.Location = new Point(x - 10, y);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此结果的一种方法是使用
计时器
,并在keydown
事件上启用它,并在keyup
事件上将其禁用。当movetimer.tick
发生时(在这种情况下,每25毫秒左右),请通过向左移动“一次”来处理事件,直到释放键为止。One way to achieve this outcome is to use a
Timer
and enable it onKeyDown
event and disable it onKeyUp
event. WhenMoveTimer.Tick
occurs (in this case every 25 ms or so), handle the event by moving "once" to the left until the key is released.