Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用键盘事件和计时器?_C# - Fatal编程技术网

C# 如何使用键盘事件和计时器?

C# 如何使用键盘事件和计时器?,c#,C#,我想要下一个: 当您按空格键-label1时,文本变为“向上”,几秒钟后(随机从1到5)label1。文本将变为“移除手”,然后按向上键label1。文本将变为“向下”。 我知道如何使用键盘向上键和向下键,但我不知道如何使用计时器 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Lin

我想要下一个: 当您按空格键-label1时,文本变为“向上”,几秒钟后(随机从1到5)label1。文本将变为“移除手”,然后按向上键label1。文本将变为“向下”。 我知道如何使用键盘向上键和向下键,但我不知道如何使用计时器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Timer timer = new Timer();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                label1.Text = "Down";
                timer.Interval = 5000;//5 seconds
                timer.Tick += new EventHandler(timer1_Tick);
                timer.Start();
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                label1.Text = "Up";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = "Remove";
            timer.Stop();
        }
    }
}

未测试,但类似于以下内容:

    private Timer timer = new Timer();

    private void OnKeyPress(object sender, KeyPressEventArgs e)
    {
        //check key press args for space here

        timer.Interval = 5000;//5 seconds

        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

        timer.Start();
    }

    private void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        label1.Text = "Up";
        timer.Elapsed -= timer_Elapsed;
        timer.Stop();
    }

基本上,interval属性以毫秒为单位,这是指定要等待多长时间的地方。然后将事件处理程序添加到经过的事件中。这将在调用Start()方法后经过指定的时间段后触发。

KeyDown
将在用户不释放时继续触发,因此您可能需要一个变量来仅启动计时器一次

private Random rnd = new Random();
private bool _SpacePressed = false;

public Form1() {
  InitializeComponent();
  this.KeyPreview = true;
  label1.Text = "Down";
  timer1.Tick += new EventHandler(timer1_Tick);    
}

void timer1_Tick(object sender, EventArgs e) {
  timer1.Stop();
  label1.Text = "Remove Hand";
}

protected override void OnKeyDown(KeyEventArgs e) {
  base.OnKeyDown(e);
  if (e.KeyCode == Keys.Space && !_SpacePressed) {
    _SpacePressed = true;
    label1.Text = "Up";
    timer1.Interval = rnd.Next(1, 5) * 1000;
    timer1.Start();
  }
}

protected override void OnKeyUp(KeyEventArgs e) {
  base.OnKeyUp(e);
  if (_SpacePressed) {
    _SpacePressed = false;
    timer1.Stop();
    label1.Text = "Down";
  }
}

你试过什么?看看这门课。特别是过去的事件可能会引起你的兴趣。我试过这个。。但它不起作用。请参阅上面的代码。上面的代码中有什么不起作用?你期待什么,发生了什么(或没有发生什么)。你必须给我们提供更多的信息(为你)。我不确定解释代码是否比给出一个没有任何注释的完整源代码示例更能帮助annayak。与其使用变量来保持按键状态,不如使用KeyPress事件。@Skalli
KeyPress
将继续触发,安纳亚克:你清楚这是怎么回事吗?我这样问是因为如果你这样做了,对你更有价值,以防你遇到下一个问题。@LarsTech:的确,我把一些事情弄混了。很抱歉