Autor Beitrag
Max064
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52



BeitragVerfasst: Mo 09.08.10 13:43 
Hi,

ich benutze verschiedene MouseEvents in einem Programm. Jedoch ist innerhalb eines MouseEventHandlers wichtig, ob eine bestimmte Tastaturtaste gedrückt ist oder nicht.

Bisherige Eventhandler:
ausblenden C#-Quelltext
1:
2:
3:
4:
private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
...
}

Wie kann ich innerhalb dieses EventHandlers abfragen, ob eine Taste gedrückt ist?

lg Max
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 09.08.10 15:16 
Die Sondertasten (Ctrl, Shift, Alt) kannst du über die statische Variable Control.ModifierKeys abfragen.

Andere Tasten kannst du im KeyUp/Down-Ereignis des Forms als lokale (bool)-Variable anlegen und abfragen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
private bool key_pressed = false;

void form_KeyDown(object sender, KeyEventArgs e)
{
  if(e.Key == Keys.T) // <- hier andere Taste anstatt 'T' eintragen
    key_pressed = true;
}

void form_KeyUp(object sender, KeyEventArgs e)
{
  if(e.Key == Keys.T) // dito
    key_pressed = false;
}

void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
  if(key_pressed)
  {
    ...
  }
}

Beachte: Die Eigenschaft 'KeyPreview' bei deiner Form sollte (bzw. muß) dann 'true' sein!

(bestimmte Sondertasten wie Tab, Return, ESC kannst du so jedoch nicht abfragen; da benötigst du dann andere Methoden wie z.B. IsInputKey()).

Für diesen Beitrag haben gedankt: Max064
Max064 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52



BeitragVerfasst: Di 10.08.10 09:17 
Ich bin über den Control.ModifierKeys gegangen. Funktioniert super! Danke.