Quantcast
Viewing all articles
Browse latest Browse all 168

SendInput - Function to type ASCII characters - where's the flaw?

Dear friends,

I need to fill a "File Open" dialog box with a string (file path and name).

Since my language has plenty of characters on the extended ASCII - for instance, a file can be named "C:\Hipóteses\Ações de mínima freqüência.doc", i thought of sending all characters in the format "Alt+000" instead of mapping each keyboard combination that produces it. The code to do this follows at the end.

However, this combination doesn't seem to work. I tested it upon a TextBox, and it caught KeyDown and KeyUp events correctly, but the due character doesn't appear :(. Can any of you friends spot the reason?

Thank you very much!

(My specs: VS/VB 2010, .NET 4.0, Windows 8)

vb Code:
  1. Imports System.Runtime.InteropServices
  2. Namespace WinApi
  3.     Module WinApi
  4.         Public Function TypeAscii(ByVal code As Integer) As Boolean
  5.             Dim kc = 96 + (code \ 100) ' key from numeric keyboard, for 1st digit of ASCII code
  6.             Dim kd = 96 + ((code Mod 100) \ 10) ' key from numeric keyboard, for 2nd digit of ASCII code
  7.             Dim ku = 96 + (code Mod 10) ' key from numeric keyboard, for 3rd digit of ASCII code
  8.             Return SendSingleInput(InputKeyDown(18)) AndAlso
  9.                    SendSingleInput(InputKeyDown(kc)) AndAlso
  10.                    SendSingleInput(InputKeyUp(kc)) AndAlso
  11.                    SendSingleInput(InputKeyDown(kd)) AndAlso
  12.                    SendSingleInput(InputKeyUp(kd)) AndAlso
  13.                    SendSingleInput(InputKeyDown(ku)) AndAlso
  14.                    SendSingleInput(InputKeyUp(ku)) AndAlso
  15.                    SendSingleInput(InputKeyUp(18))
  16.         End Function
  17.         Private Function SendSingleInput(ByVal single_input As INPUT) As Boolean
  18.             Return CBool(SendInput(1, single_input, Marshal.SizeOf(single_input)))
  19.             Application.DoEvents()
  20.         End Function
  21.         Private Function InputKeyDown(ByVal wVk As Short) As INPUT
  22.             Dim i As INPUT
  23.             i.type = INPUT_KEYBOARD
  24.             i.u.ki.wVk = wVk
  25.             i.u.ki.dwFlags = KEYEVENTF_KEYDOWN
  26.             Return i
  27.         End Function
  28.         Private Function InputKeyUp(ByVal wVk As Short) As INPUT
  29.             Dim i As INPUT
  30.             i.type = INPUT_KEYBOARD
  31.             i.u.ki.wVk = wVk
  32.             i.u.ki.dwFlags = KEYEVENTF_KEYUP
  33.             Return i
  34.         End Function
  35.         ' From here on, it's not "my" code, but code taken from Pinvoke.net and other places
  36.         <DllImport("user32.dll", SetLastError:=True)> _
  37.         Private Function SendInput(ByVal cInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
  38.         End Function
  39.         Structure INPUT
  40.             Public type As Integer
  41.             Public u As InputUnion
  42.         End Structure
  43.         Private Const KEYEVENTF_KEYDOWN As Integer = &H0
  44.         Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
  45.         Private Const KEYEVENTF_KEYUP As Integer = &H2
  46.         Private Const KEYEVENTF_UNICODE As Integer = &H4
  47.         Private Const KEYEVENTF_SCANCODE As Integer = &H8
  48.         Private Const INPUT_MOUSE As Integer = 0
  49.         Private Const INPUT_KEYBOARD As Integer = 1
  50.         Private Const INPUT_HARDWARE As Integer = 2
  51.         <StructLayout(LayoutKind.Explicit)> _
  52.         Structure InputUnion
  53.             <FieldOffset(0)> Public mi As MOUSEINPUT
  54.             <FieldOffset(0)> Public ki As KEYBDINPUT
  55.             <FieldOffset(0)> Public hi As HARDWAREINPUT
  56.         End Structure
  57.         Structure MOUSEINPUT
  58.             Public dx As Integer
  59.             Public dy As Integer
  60.             Public mouseData As Integer
  61.             Public dwFlags As Integer
  62.             Public time As Integer
  63.             Public dwExtraInfo As IntPtr
  64.         End Structure
  65.         Private Const MOUSEEVENTF_MOVE = &H1
  66.         Private Const MOUSEEVENTF_LEFTDOWN = &H2
  67.         Private Const MOUSEEVENTF_LEFTUP = &H4
  68.         Private Const MOUSEEVENTF_RIGHTDOWN = &H8
  69.         Private Const MOUSEEVENTF_RIGHTUP = &H10
  70.         Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
  71.         Private Const MOUSEEVENTF_MIDDLEUP = &H40
  72.         Private Const MOUSEEVENTF_ABSOLUTE = &H8000
  73.         Structure KEYBDINPUT
  74.             Public wVk As Short
  75.             Public wScan As Short
  76.             Public dwFlags As Integer
  77.             Public time As Integer
  78.             Public dwExtraInfo As IntPtr
  79.         End Structure
  80.         Structure HARDWAREINPUT
  81.             Public uMsg As Integer
  82.             Public wParamL As Short
  83.             Public wParamH As Short
  84.         End Structure
  85.     End Module
  86. End Namespace

Viewing all articles
Browse latest Browse all 168

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>