kein Unterstrich beim Senden von Text

whitebandit

Grünschnabel
Hallo zusammen!

Ich habe ein großes Problem. Ich versuche schon über einen längeren Zeitraum einme externen Programm was zu schicken. NEIN nicht über senkkeys sonder über eine API Schnittstelle.
Achso ich mach das Ganze in VB.NET VS2008 eine Konsolen Anwendung.
So weit so gut. Das funktioniert auch bis zu einem gewissen grad, aber jetzt kommt mein Problem. Ich habe versucht einen Unterstrich zu Versenden, den ich Brauch, damit ich was anderes Starten kann.
Anbei mein Code:

Module Module1
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

Private Const KEYEVENTF_KEYUP As Integer = &H2
Public procid As Int16
' Virtual KeyCodes
Private Enum eVirtualKeyCode
VK_BAK = &H8
VK_TAB = &H9
VK_CLEAR = &HC
VK_RETURN = &HD
VK_SHIFT = &H10
VK_CONTROL = &H11
VK_MENU = &H12
VK_PAUSE = &H13
VK_CAPITAL = &H14
VK_ESCAPE = &H1B
VK_PRIOR = &H21
VK_NEXT = &H22
VK_END = &H23
VK_HOME = &H24
VK_LEFT = &H25
VK_UP = &H26
VK_RIGHT = &H27
VK_DOWN = &H28
VK_SELECT = &H29
VK_SNAPSHOT = &H2C ' NEU! Windows-Taste
VK_INSERT = &H2D
VK_DELETE = &H2E
VK_HELP = &H2F
VK_F1 = &H70
VK_F2 = &H71
VK_F3 = &H72
VK_F4 = &H73
VK_F5 = &H74
VK_F6 = &H75
VK_F7 = &H76
VK_F8 = &H77
VK_F9 = &H78
VK_F10 = &H79
VK_F11 = &H7A
VK_F12 = &H7B
VK_F13 = &H7C
VK_F14 = &H7D
VK_F15 = &H7E
VK_F16 = &H7F
VK_NUMLOCK = &H90
VK_SCROLL = &H91
VK_WIN = &H5B ' NEU! Windows-Taste
VK_APPS = &H5D ' NEU! Taste für Kontextmenü
End Enum

Public Sub SendKeysEx(ByVal sText As String)
Dim VK As eVirtualKeyCode
Dim sChar As String
Dim i As Short
Dim bShift As Boolean

' Jedes Zeichen einzeln senden
For i = 1 To Len(sText)
' aktuelles Zeichen extrahieren
sChar = Mid(sText, i, 1)

' Sonderzeichen?
bShift = False
If sChar = "{" Then
If UCase(Mid(sText, i + 1, 9)) = "BACKSPACE" Then
VK = eVirtualKeyCode.VK_BAK
i = i + 9
ElseIf UCase(Mid(sText, i + 1, 2)) = "BS" Then
VK = eVirtualKeyCode.VK_BAK
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 4)) = "BKSP" Then
VK = eVirtualKeyCode.VK_BAK
i = i + 5
ElseIf UCase(Mid(sText, i + 1, 5)) = "BREAK" Then
VK = eVirtualKeyCode.VK_PAUSE
i = i + 6
ElseIf UCase(Mid(sText, i + 1, 8)) = "CAPSLOCK" Then
VK = eVirtualKeyCode.VK_CAPITAL
i = i + 9
ElseIf UCase(Mid(sText, i + 1, 6)) = "DELETE" Then
VK = eVirtualKeyCode.VK_DELETE
i = i + 7
ElseIf UCase(Mid(sText, i + 1, 3)) = "DEL" Then
VK = eVirtualKeyCode.VK_DELETE
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 4)) = "DOWN" Then
VK = eVirtualKeyCode.VK_DOWN
i = i + 5
ElseIf UCase(Mid(sText, i + 1, 2)) = "UP" Then
VK = eVirtualKeyCode.VK_UP
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 4)) = "LEFT" Then
VK = eVirtualKeyCode.VK_LEFT
i = i + 5
ElseIf UCase(Mid(sText, i + 1, 5)) = "RIGHT" Then
VK = eVirtualKeyCode.VK_RIGHT
i = i + 6
ElseIf UCase(Mid(sText, i + 1, 3)) = "END" Then
VK = eVirtualKeyCode.VK_END
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 5)) = "ENTER" Then
VK = eVirtualKeyCode.VK_RETURN
i = i + 6
ElseIf UCase(Mid(sText, i + 1, 4)) = "HOME" Then
VK = eVirtualKeyCode.VK_HOME
i = i + 5
ElseIf UCase(Mid(sText, i + 1, 3)) = "ESC" Then
VK = eVirtualKeyCode.VK_ESCAPE
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 4)) = "HELP" Then
VK = eVirtualKeyCode.VK_HELP
i = i + 5
ElseIf UCase(Mid(sText, i + 1, 6)) = "INSERT" Then
VK = eVirtualKeyCode.VK_INSERT
i = i + 7
ElseIf UCase(Mid(sText, i + 1, 3)) = "INS" Then
VK = eVirtualKeyCode.VK_INSERT
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 7)) = "NUMLOCK" Then
VK = eVirtualKeyCode.VK_NUMLOCK
i = i + 8
ElseIf UCase(Mid(sText, i + 1, 4)) = "PGUP" Then
VK = eVirtualKeyCode.VK_PRIOR
i = i + 5
ElseIf UCase(Mid(sText, i + 1, 4)) = "PGDN" Then
VK = eVirtualKeyCode.VK_NEXT
i = i + 5
ElseIf UCase(Mid(sText, i + 1, 10)) = "SCROLLLOCK" Then
VK = eVirtualKeyCode.VK_SCROLL
i = i + 11
ElseIf UCase(Mid(sText, i + 1, 3)) = "TAB" Then
VK = eVirtualKeyCode.VK_TAB
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 2)) = "F1" Then
VK = eVirtualKeyCode.VK_F1
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F2" Then
VK = eVirtualKeyCode.VK_F2
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F3" Then
VK = eVirtualKeyCode.VK_F3
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F4" Then
VK = eVirtualKeyCode.VK_F4
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F5" Then
VK = eVirtualKeyCode.VK_F5
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F6" Then
VK = eVirtualKeyCode.VK_F6
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F7" Then
VK = eVirtualKeyCode.VK_F7
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F8" Then
VK = eVirtualKeyCode.VK_F8
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 2)) = "F9" Then
VK = eVirtualKeyCode.VK_F9
i = i + 3
ElseIf UCase(Mid(sText, i + 1, 3)) = "F10" Then
VK = eVirtualKeyCode.VK_F10
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 3)) = "F11" Then
VK = eVirtualKeyCode.VK_F11
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 3)) = "F12" Then
VK = eVirtualKeyCode.VK_F12
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 3)) = "F13" Then
VK = eVirtualKeyCode.VK_F13
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 3)) = "F14" Then
VK = eVirtualKeyCode.VK_F14
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 3)) = "F15" Then
VK = eVirtualKeyCode.VK_F15
i = i + 4
ElseIf UCase(Mid(sText, i + 1, 3)) = "F16" Then
VK = eVirtualKeyCode.VK_F16
i = i + 4

' NEU! Windows-Taste
ElseIf UCase(Mid(sText, i + 1, 3)) = "WIN" Then
VK = eVirtualKeyCode.VK_WIN
i = i + 4

' NEU! Kontextmenü
ElseIf UCase(Mid(sText, i + 1, 4)) = "APPS" Then
VK = eVirtualKeyCode.VK_APPS
i = i + 5

' NEU! PrintScreen-Taste (DRUCK)
ElseIf UCase(Mid(sText, i + 1, 5)) = "PRINT" Then
VK = eVirtualKeyCode.VK_SNAPSHOT
i = i + 6
End If

ElseIf sChar = "+" Then
' Umschalttaste
VK = eVirtualKeyCode.VK_SHIFT

ElseIf sChar = "%" Then
' ALT
VK = eVirtualKeyCode.VK_MENU

ElseIf sChar = "^" Then
' STRG
VK = eVirtualKeyCode.VK_CONTROL

Else
' Großbuchstabe...?
bShift = (UCase(sChar) = sChar And Not IsNumeric(sChar))
If bShift Then
' ... dann zusätzlich Shift (Umsch)-Taste "drücken"
keybd_event(eVirtualKeyCode.VK_SHIFT, 1, 0, 0)
End If

' Virtual KeyCode ermitteln...
VK = Asc(UCase(sChar))
End If

' niederdrücken und wieder loslassen
keybd_event(VK, 1, 0, 0)
keybd_event(VK, 1, KEYEVENTF_KEYUP, 0)

' Shift (Umsch)-Taste wieder loslassen
If bShift Then
keybd_event(eVirtualKeyCode.VK_SHIFT, 1, KEYEVENTF_KEYUP, 0)
End If
Next i
End Sub



Sub Main()
Dim prozess As Integer = Shell("C:\Programme\Microsoft Office\Office10\WINWORD.EXE")
Threading.Thread.Sleep(1000)
AppActivate(prozess)
Threading.Thread.Sleep(1000)
SendKeysEx("bla_bla")
End Sub
End Module



Wie ihr in der dritt letzen Zeile seht, versende ich einen Unterstrich. Aber den wird einfach ignoriert. Bitte helft mir. Achso der Code ist so lauffähig in einer Consolen Anwendung unter VS2008
 

martinpriebe

Erfahrenes Mitglied
Lösung

Hi,


einmal brauchst du das hier ...

//Sind zwar gleich aber zum entscheiden ob shift gedrückt werden soll hilfreich
VK_UNDERSCORE = 189
VK_Minus = 189

ElseIf sChar = "_" Then
VK = eVirtualKeyCode.VK_UNDERSCORE
ElseIf sChar = "-" Then
VK = eVirtualKeyCode.VK_Minus

ElseIf sChar = "+" Then
......


und dann noch vor dem Drücken bei Underscore Shift drücken:

If VK = eVirtualKeyCode.VK_UNDERSCORE Then
keybd_event(&H10, 1, 0, 0) //Shift
End If

keybd_event(VK, 1, 0, 0)
keybd_event(VK, 1, KEYEVENTF_KEYUP, 0)

das wars
jetzt gehen auch '-' und '_'


P.S.
Falls noch Zeichen fehlen einfach das hier in die main ..

While (True)
Dim a As System.ConsoleKeyInfo
a = Console.ReadKey()
End While

und wenn du dir a dann anschaust siehst du den Code in dez.
sonst auskommentieren.

mfg
Martin
 

Neue Beiträge