我想禁用密钥 X 秒

I want to disable key for X seconds

我正在玩你要避开障碍物的游戏。我做了一个慢动作的关键。当玩家按下它时,它会保持活动状态 3 秒,之后我想禁用该键 6 秒以防止他再次按下它,我不知道该怎么做。

为此使用计时器:

System.Timers.Timer t = new System.Timers.Timer();
t.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
t.Interval=3000;
t.Enabled=true;
aButton.Enabled = false;

 private static void OnTimedEvent(object source, ElapsedEventArgs e)
 {
     aButton.Enabled = true;
     (source as Timer).Enabled = false;
 }

使用 Timer class.

public static void Main()
{
    myButton.Enabled = false;
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Elapsed +=new ElapsedEventHandler(OnTimedEvent);
    timer.Interval = 5000; // Specify the timespan
    timer.Enabled = true;         
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
     myButton.Enabled = true;
}

您可以将某些处理程序添加到 Elapsed 事件并指定一些时间跨度(以毫秒为单位),之后每个处理程序都会被调用。