当前位置: 动力学知识库 > 问答 > 编程问答 >

wpf - c# Key Events Not Working

问题描述:

My question is: Why is the KeyUp Event being called with the D key as shown below.

There seems to be no reason for this keyup event to be called. Is it something to do with an inability to handle multiple key events??

Console Output (shows false keydown being called)

Keydown: D ---> D Key pressed and held on keyboard

Keyup: D ---> D KEY NOT RELEASED ON KEYBOARD

Keydown: S ---> S Key pressed on keyboard

Keydown: D ---> D Key being held as before (repeat calls should be omitted in this)

Keyup: S ---> S Released on keyboard

Keyup: D ---> D Released on keyboard

Here is the code:

XAML:

<Window x:Class="WpfApplication1.MainWindow"

...

Closing="Window_Closing"

Title="MainWindow" Height="588" Width="816"

KeyDown="Event_KeyDown"

KeyUp="Event_KeyUp"

MouseLeftButtonDown= "Event_MouseDown" ResizeMode="CanMinimize">

<Canvas x:Name="canvas"/>

KeyDown:

public void Event_KeyDown(object sender, KeyEventArgs e)

{

if (inGame)

{

if (!e.IsRepeat) { System.Console.WriteLine("Keydown: " + e.Key.ToString()); }

if (e.Key == Key.D || e.Key == Key.Right)

{

player.right = true;

}

if (e.Key == Key.A || e.Key == Key.Left)

{

player.left = true;

}

if (e.Key == Key.W || e.Key == Key.Up)

{

player.jump();

}

if (e.Key == Key.Space)

{

flying = true;

}

if (e.Key == Key.S)

{

player.falling = true;

}

}

}

KeyUp:

public void Event_KeyUp(object sender, KeyEventArgs e)

{

if (inGame)

{

System.Console.WriteLine("Keyup: " + e.Key.ToString());

if (e.Key == Key.S)

{

player.falling = false;

}

if (e.Key == Key.D)

{

player.right = false;

}

if (e.Key == Key.A || e.Key == Key.Left)

{

player.left = false;

}

if (e.Key == Key.Space)

{

flying = false;

}

if (e.Key == Key.Escape)

{

inGame = false;

mainMenu = true;

}

}

}

Thanks,

分享给朋友:
您可能感兴趣的文章:
随机阅读: