Solved

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

Posted on 2004-08-19
9
2,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

615 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