Hallo alle zusammen,
ich verzweifel momentan daran, an einem Spiel (DirectX-Spiel) eine Taste zu senden.
Ich habe inzwischen alle Beispiele die ich finden konnte versucht, und tatsächlich nur eines gefunden, welches offenbar funktionieren sollte / es aber nicht macht → Jedoch handelt es sich hierbei um eine andere Sprache (C / C++).
Mich würde nun Interessieren ob jemand vielleicht bei den verschiedenen Möglichkeiten noch eine Idee hat oder vielleicht einen Fehler findet oder eine ganz andere Methode noch kennt:
Bei jedem Aufruf der Funktion „KeySend“ war selbstverständlich das Fenster des Spiels immer Aktiv.
Bei der ersten Möglichkeit handelt es sich um den Code welcher Funktionieren sollte und für mich in einem Forum von C / C++ in VB.Net übersetzt wurde. Wenn ich nun die Funktion aufrufe passiert nichts - im Windows kommt keine Taste an und auch in dem Spiel (Fenster natürlich aktiv).
Inwieweit die Übersetzung korrekt ist, kann ich leider nicht beurteilen!
Ich selbst benutze Microsoft Windows Vista 64 Bit / Michrosoft Visual Basic 2008 Express
Module KeySend1
' usually I am not releasing my stuff. But here is a small hint for
'"do it yourself" people, how to simulate keystrokes.
'The usual way to do that via Postmessage or kbdevent does NOT work here,
'because is a DirectInput game.
'It cost me some hours to figure out a proper but easy way how to do it:
'INPUT input;
'memset(&input,0,sizeof(INPUT));
'input.type=INPUT_KEYBOARD;
'input.ki.wScan =DIKEYBOARD_1; // direct-input scancode for key '1'
'if(press_key) input.ki.dwFlags=0;
'else input.ki.dwFlags=KEYEVENTF_KEYUP;
'SendInput(1,&input,sizeof(INPUT));
'This example will properly work for XT and Vista.
'The trick is simply NOT to use VK_'s, but the lower 16 bits of DirectX _
scancodes.
'Try to google for DIKEYBOARD_1 and you will find them all.
'Have Fun with it.
'PS: It seems to be hard to find the right one, so I post those scancodes
' here:
'#ifndef DIRECT_X_KEYS_HPP_DEFINED
'#define DIRECT_X_KEYS_HPP_DEFINED
'#define DIKEYBOARD_ESCAPE 0x0401 //
'#define DIKEYBOARD_1 0x0402 //
'#define DIKEYBOARD_2 0x0403 //
'#define DIKEYBOARD_3 0x0404 //
'#endif
Private Declare Function SendInput Lib "user32.dll" (ByVal cInputs As Long, _
ByRef pInputs As Input, ByVal cbSize As Long) As Long
Private Structure KEYBDINPUT
Dim wVk As Integer
Dim wScan As Integer
Dim dwFlags As Long
Dim time As Long
Dim dwExtraInfo As Long
Dim unused1 As Long
Dim unused2 As Long
End Structure
Private Structure Input
Dim Type As Long
Dim ki As KEYBDINPUT
End Structure
Private Const INPUT_KEYBOARD As Long = 1
Private Const DIKEYBOARD_1 As Long = &H402
Private Const KEYEVENTF_KEYUP As Long = &H2
Public Sub KeySend()
Dim inp As Input
'Taste drücken
inp.Type = INPUT_KEYBOARD
inp.ki.wScan = DIKEYBOARD_1
inp.ki.dwFlags = 0
SendInput(1, inp, Len(inp))
'Taste loslassen
inp.Type = INPUT_KEYBOARD
inp.ki.wScan = DIKEYBOARD_1
inp.ki.dwFlags = KEYEVENTF_KEYUP
SendInput(1, inp, Len(inp))
End Sub
End Module Bei der zweiten möglichkeit die ich im Internet gefunden habe handelt es sich um die keybd_event API – Hier kommen die Tasten im Windows an (z.b. Explorer Adressleiste, Notepad) aber nicht im Spiel auch wenn ich das Chat-Fenster aktiviere.
Module KeySend3
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYBOARD_1 As Byte = &H49
Private Const KEYEVENTF_KEYUP As Byte = &H2
Public Sub KeySend()
'Taste drücken
keybd_event(KEYBOARD_1, 0, 0, 0)
'Taste loslassen
keybd_event(KEYBOARD_1, 0, KEYEVENTF_KEYUP, 0)
End Sub
End Module |