Solved

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

Posted on 2004-08-19
9
2,078 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now