bankaifan
Goto Top

GetASyncKeyState in CSharp 2.0

Hierbei geht es darum die gedrückten Tasten in der Gesamten Windowsumgebung abzufangen.

Als erstes erstellt ihr eine Klasse und fügt dort folgenden Code ein

[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]  
		public static extern long GetAsyncKeyState(long vKey);
		
		public static int AllKeys = {VK_0,VK_1,VK_2,VK_3,VK_4,VK_5,VK_6,VK_7,VK_8,VK_9,VK_A,VK_ADD,VK_ATTN,VK_B,VK_B,VK_BACK,VK_C,VK_CAPITAL,VK_CLEAR,VK_CONTEXTKEY,VK_CONTROL,VK_CRSEL,VK_D,VK_DECIMAL,VK_DELETE,VK_DIVIDE,VK_DOWN,VK_E,VK_END,VK_EREOF,VK_ESCAPE,VK_EXSEL,VK_F,VK_F1,VK_F10,
			VK_F11,VK_F12,VK_F13,VK_F14,VK_F15,VK_F16,VK_F17,VK_F18,VK_F19,VK_F2,VK_F20,VK_F21,VK_F22,VK_F23,VK_F24,VK_F3,VK_F4,VK_F5,VK_F6,VK_F7,VK_F8,VK_F9,VK_G,VK_H,VK_HELP,VK_HOME,VK_I,VK_ICO_00,VK_ICO_CLEAR,VK_ICO_F17,VK_ICO_F18,VK_ICO_HELP,VK_INSERT,VK_J,VK_K,VK_L,VK_LBUTTON,
			VK_LCONTROL,VK_LEFT,VK_LMENU,VK_LSHIFT,VK_M,VK_MBUTTON,VK_MENU,VK_MULTIPLY,VK_N,VK_NEXT,VK_NONAME,VK_NUMLOCK,VK_NUMPAD0,VK_NUMPAD1,VK_NUMPAD2,VK_NUMPAD3,VK_NUMPAD4,VK_NUMPAD5,VK_NUMPAD6,VK_NUMPAD7,VK_NUMPAD8,VK_NUMPAD8,VK_NUMPAD9,VK_O,VK_OEM_1,VK_OEM_2,VK_OEM_3,VK_OEM_4,
			VK_OEM_5,VK_OEM_6,VK_OEM_7,VK_OEM_ATTN,VK_OEM_AUTO,VK_OEM_BACKTAB,VK_OEM_CLEAR,VK_OEM_COMMA,VK_OEM_COPY,VK_OEM_CUSEL,VK_OEM_ENLW,VK_OEM_FINNISH,VK_OEM_JUMP,VK_OEM_MINUS,VK_OEM_PA1,VK_OEM_PA2,VK_OEM_PA3,VK_OEM_PERIOD,VK_OEM_PLUS,VK_OEM_RESET,VK_OEM_SCROLL,VK_OEM_WSCTRL,VK_OEM102,
			VK_P,VK_PA1,VK_PAUSE,VK_PLAY,VK_PRINT,VK_PRIOR,VK_Q,VK_R,VK_RBUTTON,VK_RCONTROL,VK_RETURN,VK_RIGHT,VK_RMENU,VK_RSHIFT,VK_S,VK_SHIFT,VK_SNAPSHOT,VK_SPACE,VK_STARTKEY,VK_SUBTRACT,VK_T,VK_TAB,VK_U,VK_UP,VK_V,VK_W,VK_X,VK_Y,VK_Z,VK_ZOOM};
		// GetAsyncKeyState vKey-Konstanten
			// Linker Mausbutton
		public const int VK_LBUTTON = 0x1;
			// Rechter Mausbutton
		public const int VK_RBUTTON = 0x2;
			// Mittlerer Mausbutton
		public const int VK_MBUTTON = 0x4;
			// Backspace Taste
		public const int VK_BACK = 0x8;
			// Tab Taste
		public const int VK_TAB = 0x9;
			// Numpad 5 ohne Numlock
		public const int VK_CLEAR = 0xc;
			// Enter Taste
		public const int VK_RETURN = 0xd;
			// Shift Taste
		public const int VK_SHIFT = 0x10;
			// STRG Taste
		public const int VK_CONTROL = 0x11;
			// Alt Taste
		public const int VK_MENU = 0x12;
			// Pause/Untbr
		public const int VK_PAUSE = 0x13;
			// Caps Lock/Feststelltaste
		public const int VK_CAPITAL = 0x14;
			// Escape
		public const int VK_ESCAPE = 0x1b;
			// Space/Leertaste
		public const int VK_SPACE = 0x20;
			// PageUp/Bild hoch
		public const int VK_PRIOR = 0x21;
			// PageDown/Bild runter
		public const int VK_NEXT = 0x22;
			// Ende
		public const int VK_END = 0x23;
			// Home/Pos1
		public const int VK_HOME = 0x24;
			// Linke Pfeiltaste
		public const int VK_LEFT = 0x25;
			// Obere Pfeiltaste
		public const int VK_UP = 0x26;
			// Rechte Pfeiltaste
		public const int VK_RIGHT = 0x27;
			// Untere Pfeiltaste
		public const int VK_DOWN = 0x28;
			// Drucken (Nokia Tastaturen)
		public const int VK_PRINT = 0x2a;
			// Drucken/S-Abf
		public const int VK_SNAPSHOT = 0x2c;
			// Einfügen
		public const int VK_INSERT = 0x2d;
			// Entfernen
		public const int VK_DELETE = 0x2e;
			// Hilfe
		public const int VK_HELP = 0x2f;
			// Taste 0
		public const int VK_0 = 0x30;
			// Taste 1
		public const int VK_1 = 0x31;
			// Taste 2
		public const int VK_2 = 0x32;
			// Taste 3
		public const int VK_3 = 0x33;
			// Taste 4
		public const int VK_4 = 0x34;
			// Taste 5
		public const int VK_5 = 0x35;
			// Taste 6
		public const int VK_6 = 0x36;
			// Taste 7
		public const int VK_7 = 0x37;
			// Taste 8
		public const int VK_8 = 0x38;
			// Taste 9
		public const int VK_9 = 0x39;
			// Taste A
		public const int VK_A = 0x41;
			// Taste B
		public const int VK_B = 0x42;
			// Taste C
		public const int VK_C = 0x43;
			// Taste D
		public const int VK_D = 0x44;
			// Taste E
		public const int VK_E = 0x45;
			// Taste F
		public const int VK_F = 0x46;
			// Taste G
		public const int VK_G = 0x47;
			// Taste H
		public const int VK_H = 0x48;
			// Taste I
		public const int VK_I = 0x49;
			// Taste J
		public const int VK_J = 0x4a;
			// Taste K
		public const int VK_K = 0x4b;
			// Taste L
		public const int VK_L = 0x4c;
			// Taste M
		public const int VK_M = 0x4d;
			// Taste N
		public const int VK_N = 0x4e;
			// Taste O
		public const int VK_O = 0x4f;
			// Taste P
		public const int VK_P = 0x50;
			// Taste Q
		public const int VK_Q = 0x51;
			// Taste R
		public const int VK_R = 0x52;
			// Taste S
		public const int VK_S = 0x53;
			// Taste T
		public const int VK_T = 0x54;
			// Taste U
		public const int VK_U = 0x55;
			// Taste V
		public const int VK_V = 0x56;
			// Taste W
		public const int VK_W = 0x57;
			// Taste X
		public const int VK_X = 0x58;
			// Taste Y
		public const int VK_Y = 0x59;
			// Taste Z
		public const int VK_Z = 0x5a;
			// Startmenütaste
		public const int VK_STARTKEY = 0x5b;
			// Kentextmenü
		public const int VK_CONTEXTKEY = 0x5d;
			// Numpad Taste 0
		public const int VK_NUMPAD0 = 0x60;
			// Numpad Taste 1
		public const int VK_NUMPAD1 = 0x61;
			// Numpad Taste 2
		public const int VK_NUMPAD2 = 0x62;
			// Numpad Taste 3
		public const int VK_NUMPAD3 = 0x63;
			// Numpad Taste 4
		public const int VK_NUMPAD4 = 0x64;
			// Numpad Taste 5
		public const int VK_NUMPAD5 = 0x65;
			// Numpad Taste 6
		public const int VK_NUMPAD6 = 0x66;
			// Numpad Taste 7
		public const int VK_NUMPAD7 = 0x67;
			// Numpad Taste 8
		public const int VK_NUMPAD8 = 0x68;
			// Numpad Taste 9
		public const int VK_NUMPAD9 = 0x69;
			// Numpad Multiplikations Taste (*)
		public const int VK_MULTIPLY = 0x6a;
			// Numpad Additions Taste (+)
		public const int VK_ADD = 0x6b;
			// Numpad Subtrations Taste (-)
		public const int VK_SUBTRACT = 0x6d;
			// Numpad Komma Taste (,)
		public const int VK_DECIMAL = 0x6e;
			// Numpad Devidierungs Taste (/)
		public const int VK_DIVIDE = 0x6f;
			// F1 Taste
		public const int VK_F1 = 0x70;
			// F2 Taste
		public const int VK_F2 = 0x71;
			// F3 Taste
		public const int VK_F3 = 0x72;
			// F4 Taste
		public const int VK_F4 = 0x73;
			// F5 Taste
		public const int VK_F5 = 0x74;
			// F6 Taste
		public const int VK_F6 = 0x75;
			// F7 Taste
		public const int VK_F7 = 0x76;
			// F8 Taste
		public const int VK_F8 = 0x77;
			// F9 Taste
		public const int VK_F9 = 0x78;
			// F10 Taste
		public const int VK_F10 = 0x79;
			// F11 Taste
		public const int VK_F11 = 0x7a;
			// F12 Taste
		public const int VK_F12 = 0x7b;
			// F13 Taste
		public const int VK_F13 = 0x7c;
			// F14 Taste
		public const int VK_F14 = 0x7d;
			// F15 Taste
		public const int VK_F15 = 0x7e;
			// F16 Taste
		public const int VK_F16 = 0x7f;
			// F17 Taste
		public const int VK_F17 = 0x80;
			// F18 Taste
		public const int VK_F18 = 0x81;
			// F19 Taste
		public const int VK_F19 = 0x82;
			// F20 Taste
		public const int VK_F20 = 0x83;
			// F21 Taste
		public const int VK_F21 = 0x84;
			// F22 Taste
		public const int VK_F22 = 0x85;
			// F23 Taste
		public const int VK_F23 = 0x86;
			// F24 Taste
		public const int VK_F24 = 0x87;
			// Numlock Taste
		public const int VK_NUMLOCK = 0x90;
			// Scroll Lock
		public const int VK_OEM_SCROLL = 0x91;
			// Linke Shift-Taste
		public const int VK_LSHIFT = 0xa0;
			// Rechte Shift-Taste
		public const int VK_RSHIFT = 0xa1;
			// Linke STRG-Taste
		public const int VK_LCONTROL = 0xa2;
			// Rechte STRG-Taste
		public const int VK_RCONTROL = 0xa3;
			// Linke ALT-Taste
		public const int VK_LMENU = 0xa4;
			// Rechte ALT-Taste
		public const int VK_RMENU = 0xa5;
			// ";"-Taste 
		public const int VK_OEM_1 = 0xba;
			// " 
		public const int VK_OEM_PLUS = 0xbb;
			// ","-Taste 
		public const int VK_OEM_COMMA = 0xbc;
			// "-"-Taste 
		public const int VK_OEM_MINUS = 0xbd;
			// "."-taste 
		public const int VK_OEM_PERIOD = 0xbe;
			// "/"-Taste 
		public const int VK_OEM_2 = 0xbf;
			// "`"-Taste 
		public const int VK_OEM_3 = 0xc0;
			// "["-Taste 
		public const int VK_OEM_4 = 0xdb;
			// "\"-Taste 
		public const int VK_OEM_5 = 0xdc;
			// "]"-Taste 
		public const int VK_OEM_6 = 0xdd;
			// " 
		public const int VK_OEM_7 = 0xde;
			// F17 einer Olivette Tastatur (Intern)
		public const int VK_ICO_F17 = 0xe0;
			// F18 einer Olivette Tastatur (Intern)
		public const int VK_ICO_F18 = 0xe1;
			// "<"-Taste oder "|"-Taste einer   
		public const int VK_OEM102 = 0xe2;
	// IBM-Kompatiblen 102 Tastatur (Nicht US)
			// Hilfetaste einer Olivetti Tastatur (Intern) 
		public const int VK_ICO_HELP = 0xe3;
			// 00-Taste einer Olivetti Tastatur (Intern)
		public const int VK_ICO_00 = 0xe4;
			// Löschen Taste einer Olivetti Tastatur (Intern) 
		public const int VK_ICO_CLEAR = 0xe6;
			// Reset Taste (Nokia)
		public const int VK_OEM_RESET = 0xe9;
			// Springen Taste (Nokia)
		public const int VK_OEM_JUMP = 0xea;
			// PA1 Taste (Nokia)
		public const int VK_OEM_PA1 = 0xeb;
			// PA2 Taste (Nokia)
		public const int VK_OEM_PA2 = 0xec;
			// PA3 Taste (Nokia)
		public const int VK_OEM_PA3 = 0xed;
			// WSCTRL Taste (Nokia)
		public const int VK_OEM_WSCTRL = 0xee;
			// WSCTRL Taste (Nokia)
		public const int VK_OEM_CUSEL = 0xef;
			// ATTN Taste (Nokia)
		public const int VK_OEM_ATTN = 0xf0;
			// Fertig Taste (Nokia)
		public const int VK_OEM_FINNISH = 0xf1;
			// Kopieren Taste (Nokia)
		public const int VK_OEM_COPY = 0xf2;
			// Auto Taste (Nokia)
		public const int VK_OEM_AUTO = 0xf3;
			// ENLW Taste (Nokia)
		public const int VK_OEM_ENLW = 0xf4;
			// BackTab Taste (Nokia)
		public const int VK_OEM_BACKTAB = 0xf5;
			// ATTN-Taste
		public const int VK_ATTN = 0xf6;
			// CRSEL-Taste
		public const int VK_CRSEL = 0xf7;
			// EXSEL-Taste
		public const int VK_EXSEL = 0xf8;
			// EREOF-Taste
		public const int VK_EREOF = 0xf9;
			// PLAY-Taste
		public const int VK_PLAY = 0xfa;
			// ZOOM-Taste
		public const int VK_ZOOM = 0xfb;
			// NONAME-Taste
		public const int VK_NONAME = 0xfc;
			// PA1-Taste
		public const int VK_PA1 = 0xfd;
			// OEM_CLEAR-Taste
		public const int VK_OEM_CLEAR = 0xfe;

Dieser enthält ein DLL Import, ein Array in dem Alle Keys zusammengefasst sind und die auflistung der Keys mit ihrer Integerzuweisung.

Anschließend könnt ihr mithilfe dieses Codes hier

		[DllImport("User32.dll")]  
		private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey); // Keys enumeration
		[DllImport("User32.dll")]  
		private static extern short GetAsyncKeyState(System.Int32 vKey);
		
		Keys checkKey()
		{
			Keys pressedKey = Keys.Pa1;
			long Key = 0;
			foreach(Keys currentKey in Class1.AllKeys)
			{
				Key = GetAsyncKeyState(currentKey);
				if (Convert.ToBoolean(Key & 0x8000))
				{
					pressedKey = currentKey;
				}
			}
			return pressedKey;
		}

Jede gedrückte Taste abfangen. Die Methode gibt einen Wert vom Typ System.Windows.Forms.Keys zurück was ihr damit macht ist euch überlassen ich verwende die Methode um sie in einem Thread aufzurufen

void UseKeyInfo()
		{
			do
			{
				Keys pressedKey = checkKey();
				if(pressedKey != Keys.Pa1)
				{
					if (pressedKey == Keys.F8)
					{
						MethodInvoker timer1Start = delegate
						{
							timer1.Start();
						};
						Invoke(timer1Start);
					}
					else if (pressedKey == Keys.F10)
					{
						MethodInvoker timer1Start = delegate
						{
							timer1.Stop();
						};
						Invoke(timer1Start);
					}
					else if (pressedKey == Keys.F9)
					{
						MethodInvoker Message = delegate
						{
							Process chat = new Process();
							chat.StartInfo.FileName="Chat.exe";  
							chat.StartInfo.WindowStyle=ProcessWindowStyle.Normal;
							try
							{
								chat.Start();
							}
							catch
							{
								if(bTest==true)
								{
									MessageBox.Show("Speichern Sie die " + Application.ProductName + ".exe in dem Ordern an dem sich der Chat befindet.","Datei nicht gefunden");  
								}
								bTest=false;
								button10.Visible = true;
							}
						};
						Invoke(Message);
					}
					pressedKey = Keys.Pa1;
				}
			}while(0==0);
		}

Ich hoffe ich konnte euch hiermit helfen und es ist leichter verständlich als meine erste Anleitung zu diesem Thema.

Liebe Grüße

Bankaifan

Content-Key: 162601

Url: https://administrator.de/contentid/162601

Ausgedruckt am: 28.03.2024 um 16:03 Uhr

Mitglied: Snowman25
Snowman25 15.03.2011 um 12:09:22 Uhr
Goto Top
Hallo @bankaifan,

Scheint mir relativ verständlich. Du hast das jetzt für ein Chatprogramm gemacht, wie ich sehe.
Lassen sich damit auch Tastendrücke abfangen, wenn sie garnicht im aktiven Fenster sind? Die Anwendung also im Hintergrund läuft?

Gruß
Snow
Mitglied: mathe172
mathe172 15.03.2011 um 21:11:35 Uhr
Goto Top
Hallo zusammen!

@Snowman25:
Lies mal die erste Zeile:
Hierbei geht es darum die gedrückten Tasten in der Gesamten Windowsumgebung abzufangen.
... face-smile

@ bankaifan: Ich kenn mich zwar mit C# nicht aus aber liesse sich die Array nicht einfach durch Keys ersetzen? Die Schleife würde ich dann so machen: Für i als Keys(oder integer) von 0 bis 255 checke GetAsyncKeyState...

Sonst:
Scheint mir relativ verständlich
face-smile

Mathe172
PS.:Snowman, ich hoffe nicht dass du an den Verwendungszweck denkst, der mir jetzt eingefallen ist nach deinem Beitrag face-wink
Mitglied: bankaifan
bankaifan 23.03.2011 um 17:22:55 Uhr
Goto Top
Moin,

Du meinst vermutlich anstelle der Const-Variablen oder?

Ich denke das liese sich machen. Ich verwende es lieber so, da ich anhand der Namen genau weis welche Taste gemeint ist.

[OT]
Ich hab mich damit beschäftigt als ich viiieell Zeit hatte :D Da hab ich ned über kürzungsmöglichkeiten nachgedacht face-wink
[/OT]