Solved

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

Posted on 2004-08-19
9
2,091 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:dvplayltd
  • 4
  • 3
  • 2
9 Comments
 

Expert Comment

by:dplus
ID: 11845410
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
 

Expert Comment

by:dplus
ID: 11845495
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
 

Author Comment

by:dvplayltd
ID: 11845640
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:dvplayltd
ID: 11845734
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
 

Assisted Solution

by:dplus
dplus earned 50 total points
ID: 11846105
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
 

Author Comment

by:dvplayltd
ID: 11846315
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
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 450 total points
ID: 11857056
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
 

Author Comment

by:dvplayltd
ID: 11857771
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 11876139
>>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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question