Simulate SendKey by API (PostMessage) for all chars in ASCII table.

Hello experts !

I need to trasnsfer char by API PostMessage. The window, that will recive messages will be VB text box.  The window that recive message do NOT have focus and it must do NOT set active.
Char can be any from ASCII table. I do NOT need to simulate CTRL, ALT, SHIFT or other special keys.

The speed is important - if is possible I prefer to use ONE message for a char.

I know that solution is
    PostMessage text1.hWnd,  WM_KEYDOWN, wParam, lParam
or
    PostMessage text1.hWnd,  WM_CHAR , wParam, lParam

BUT I CAN NOT MAKE CORRECT wParam AND lParam  VALUE THAT WORK FIND WITH ALL ASCII TABLE.

I use Visual Basic 6 with SP5, Win2000 or Win XP.
dvplayltdAsked:
Who is Participating?
 
zzzzzoocCommented:
WM_CHAR should work.


Form1:
--------------
Option Explicit

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const WM_CHAR = &H102
Private Sub SendKey(ByVal lngHandle As Long, ByVal bytKey As Byte)
    Call PostMessage(lngHandle, WM_CHAR, ByVal bytKey, ByVal 0)
End Sub
Private Sub Command1_Click()
    Dim bytChar As Byte
    'Generate a random character from about 32 to 122 (if I did it right!)
    bytChar = Int(Rnd * (122 - 32)) + 33
    'Send the character...
    Call SendKey(Text1.hwnd, bytChar)
End Sub
0
 
dplusCommented:
This should do the trick:

' Sends ascii "A" (65 decimal) to hwnd:
Call PostMessage(hwnd, WM_KEYDOWN, 65, 0)
Call PostMessage(hwnd, WM_KEYUP, 65, 0)

' Sends ascii "B" (66 decimal) to hwnd:
Call PostMessage(hwnd, WM_KEYDOWN, 66, 0)
Call PostMessage(hwnd, WM_KEYUP, 66, 0)

' Sends ascii "C" (67 decimal) to hwnd:
Call PostMessage(hwnd, WM_KEYDOWN, 67, 0)
Call PostMessage(hwnd, WM_KEYUP, 67, 0)

See http://www.techonthenet.com/ascii/chart.htm for the decimal values of each ASCII character.
0
 
dplusCommented:
Well, I just tested it myself:

Open a new project (exe), drag a textbox (text1) and commandbutton (command1) to the form and put this in your code:

Option Explicit

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long _
) As Long

Private Sub Command1_Click()

' Sends ascii "B" (66 decimal) to hwnd:
Call PostMessage(Text1.hwnd, &H100, 66, 0)
Call PostMessage(Text1.hwnd, &H101, 66, 0)

End Sub
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
dvplayltdAuthor Commented:
Dear, dplus

Are you sure that simple work ? It do NOT work for me at all.
In wParam can NOT be used directly ASCII code. It value is more complex.
0
 
dvplayltdAuthor Commented:
Dear, dplus
I have to correct my comments - your example work, but NOT with all ASCII table. Try for example to send "!" - 33 DEC, "<" -  62 DEC and you will see what I mean.
0
 
dplusCommented:
Well, you're right indeed.
You can use the following code to achieve entering the "!" in your textbox.
Again, select EXE project, add text1 and command1:



Option Explicit

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    ByVal HWND As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long _
) As Long

Private Sub Command1_Click()

Clipboard.SetText "!"

' Sends clipboard-data to hwnd:
Call PostMessage(Text1.HWND, &H302, 0, 0)

End Sub

0
 
dvplayltdAuthor Commented:
Interesting way, work good, fast, but in my situation I can have trouble.

I intead to use postmessages for exchange string messages between 5-10 ActiveX exe. The problem is that sometimes ActicveX EXE can be busy for 5-10 seconds. And imagine
"ActiveX EXE 1" - copy to clipboard text and send WM_PASTE to "ActiveX  EXE 2"
"ActiveX  EXE 2" is bisy and canNOT process PASTE commad.
"ActiveX EXE 3" start comynicate with other ActriveX EXE. And it overwrite clipboard.
Result is that when ActiveX EXE 2 is free and process PASTE clipboard can will be overwriten.

Other disadvantage is that if user works - it will be surprise by "hide" replacing of clipboard.

I will wait some days for other answers and if do NOT have I will appcept yours with half point.
In general thair a way to send all ASCII using WM_KEYDOWN and this way is best for me.
0
 
dvplayltdAuthor Commented:
Dear zzzzzooc,

Your example work OK and it exact what I need, with ony ONE exception.
I need to send ALL ASCII table. I work in Bulgaria and ASCII table with numbers bigger that 122 is used to represent Bulgarian letters.
Can I use UNICODE ? I see message WM_UNICHAR. My target is Win 2000 or Win XP.
0
 
zzzzzoocCommented:
>>with numbers bigger that 122 is used to represent Bulgarian letters
I'm guessing that's restricted to a font and it's character-set. WM_CHAR should be able to handle the entire character-set correctly.

For instance, to send the charater-codes 150 and 151:
Call SendKey(Text1.hwnd, 150)
Call SendKey(Text1.hwnd, 151)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.