Benutzer mit den meisten Antworten
Gedrückt halten einer Taste und neue Taste auswählen

Frage
-
Moin,
ich hab ein Problem bezüglich des KeyDown-Events in C#. Ich konnte da nicht wirklich bisher einen Thread finden, auch weil es für mich schwierig ist dies als Suchbegriff zu formulieren.
Und zwar möchte ich mittels des Key-Down Events dafür sorgen, dass die Taste W gedrückt bleibt. Drücke ich aber eine andere Taste z.B. "S" sollen alle anderen Tasten, die ich vorher gedrückt habe keinen Einfluss auf die Steuerung haben.
Mein Code bisher:
bool level1 = false;
bool level2 = false;
bool level3 = true;
int DelayValue;
void Wert_Verzögerung()
{
if (level1 == true)
{
DelayValue = 600;
}
if (level2 == true)
{
DelayValue = 400;
}
if (level3 == true)
{
DelayValue = 300;
}
}
void Up()
{
Wert_Verzögerung();
System.Threading.Thread.Sleep(DelayValue);
p.Location = new Point(p.Location.X, p.Location.Y - 10);
}
void Down()
{
Wert_Verzögerung();
System.Threading.Thread.Sleep(DelayValue);
p.Location = new Point(p.Location.X, p.Location.Y + 10);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
Up();
}
if (e.KeyCode == Keys.S)
{
Down();
}
}Damit will ich einen Button steuern, der sich nach links, rechts, oben und unten bewegt. Am Anfang muss ich entweder W, A, S oder D drücken. Der Button ist an einem Punkt im Spielfeld. Ich drücke die Taste und Dann fängt er an sich in eine der vier Richtung zu bewegen. Der Button soll aber in Bewegung bleiben und die Richtung gebe ich mit den genannten Tasten an.
Antworten
-
Hi,
warum soll dafür die Taste gedrückt bleiben?
Ruf deine Methoden auf, wenn W, A, S oder D gedrückt wurden (ggfs. noch ESC zum beenden aller Richtungswechsel) und in diesen aktivierst Du einen Timer, der dann periodisch auslöst und den Button verschiebt.
public partial class FormX : Form { private enum Direction { None = 0, Up = 1, Down = 2, Left = 3, Right = 4, } private System.Timers.Timer timer; private Direction lastDirection; public FormX() { InitializeComponent(); this.timer = new System.Timers.Timer(); this.timer.Interval = 500; this.timer.Elapsed += timer_TimerElapsed; this.lastDirection = Direction.None; this.KeyDown += FormX_KeyDown; } private void StartTimer() { this.timer.Start(); } private void StopTimer() { this.timer.Stop(); } private void ChangeDirection( Direction _newDirection ) { this.lastDirection = _newDirection; } private void MoveButton( Direction _direction ) { switch( _direction ) { case Direction.Up: this.Button1.Top -= 10; break; case Direction.Down: this.Button1.Top += 10; break; case Direction. this.Button1.Left -= 10; break; case Direction.Right: this.Button1.Left += 10; break; } } private void timer_TimerElapsed( Object source, System.Timers.ElapsedEventArgs e ) { this.Button1.Invoke( new Action( () => { this.MoveButton( this.lastDirection ); }) ); } private void FormX_KeyDown( object sender, KeyEventArgs e ) { switch( e.KeyCode ) { case Keys.W: this.lastDirection = Direction.Up; this.StartTimer(); break; case Keys.S: this.lastDirection = Direction.Down; this.StartTimer(); break; case Keys.A: this.lastDirection = Direction.Left; this.StartTimer(); break; case Keys.D: this.lastDirection = Direction.Right; this.StartTimer(); break; case Keys.Escape: this.lastDirection = Direction.None; this.StopTimer(); break; } } }
Ist jetzt nur so schnell dahingeschrieben, kann also noch Fehler enthalten. Aber es sollte dir zeigen, was ich meine.
Gruß, Stefan
Microsoft MVP - Visual Developer ASP/ASP.NET
http://www.asp-solutions.de/ - Consulting, Development
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
- Bearbeitet Stefan FalzModerator Montag, 23. April 2018 13:47
- Als Antwort vorgeschlagen Dimitar DenkovMicrosoft contingent staff, Administrator Mittwoch, 25. April 2018 14:29
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Administrator Freitag, 4. Mai 2018 13:34
Alle Antworten
-
Warum speicherst du nicht einfach die Taste, die zuletzt gedrückt wurde und bewegst deinen Button solange, bis eine andere Taste gedrückt wird?
Gruß
Freiberufler im Bereich Softwareentwicklung Von der PLC und Robotik zu VB.NET & C#, vorrangig WPF und UWP
-
Hi,
warum soll dafür die Taste gedrückt bleiben?
Ruf deine Methoden auf, wenn W, A, S oder D gedrückt wurden (ggfs. noch ESC zum beenden aller Richtungswechsel) und in diesen aktivierst Du einen Timer, der dann periodisch auslöst und den Button verschiebt.
public partial class FormX : Form { private enum Direction { None = 0, Up = 1, Down = 2, Left = 3, Right = 4, } private System.Timers.Timer timer; private Direction lastDirection; public FormX() { InitializeComponent(); this.timer = new System.Timers.Timer(); this.timer.Interval = 500; this.timer.Elapsed += timer_TimerElapsed; this.lastDirection = Direction.None; this.KeyDown += FormX_KeyDown; } private void StartTimer() { this.timer.Start(); } private void StopTimer() { this.timer.Stop(); } private void ChangeDirection( Direction _newDirection ) { this.lastDirection = _newDirection; } private void MoveButton( Direction _direction ) { switch( _direction ) { case Direction.Up: this.Button1.Top -= 10; break; case Direction.Down: this.Button1.Top += 10; break; case Direction. this.Button1.Left -= 10; break; case Direction.Right: this.Button1.Left += 10; break; } } private void timer_TimerElapsed( Object source, System.Timers.ElapsedEventArgs e ) { this.Button1.Invoke( new Action( () => { this.MoveButton( this.lastDirection ); }) ); } private void FormX_KeyDown( object sender, KeyEventArgs e ) { switch( e.KeyCode ) { case Keys.W: this.lastDirection = Direction.Up; this.StartTimer(); break; case Keys.S: this.lastDirection = Direction.Down; this.StartTimer(); break; case Keys.A: this.lastDirection = Direction.Left; this.StartTimer(); break; case Keys.D: this.lastDirection = Direction.Right; this.StartTimer(); break; case Keys.Escape: this.lastDirection = Direction.None; this.StopTimer(); break; } } }
Ist jetzt nur so schnell dahingeschrieben, kann also noch Fehler enthalten. Aber es sollte dir zeigen, was ich meine.
Gruß, Stefan
Microsoft MVP - Visual Developer ASP/ASP.NET
http://www.asp-solutions.de/ - Consulting, Development
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
- Bearbeitet Stefan FalzModerator Montag, 23. April 2018 13:47
- Als Antwort vorgeschlagen Dimitar DenkovMicrosoft contingent staff, Administrator Mittwoch, 25. April 2018 14:29
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Administrator Freitag, 4. Mai 2018 13:34
-
Jo danke für die Hilfe. Ich habe es angewendet und grob verstanden und ich werde mich auf darauf fokussieren ein noch besseres Verständnis zu erlangen. Bin jedoch noch nicht fortgeschritten. Viele Begriffe sind mir noch fremd. Da waren vier Begriffe in der Antwort, die mir unbekannt sind.