Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to select a text from a document using a button press in vb.net. So, i wanted to simulate the control + A key using Sendmessage() in vb.net. I tried using keybd_event and it doesn't work and even sendkeys.send() doesnot work too.

This is what i tried so far
VB
 Private Const WM_KEYDOWN = &H100
 Const VK_A = &H41
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       
        SendMessage(Handle, WM_KEYDOWN, VK_A, 0)
End sub


Please help me out!
Posted

1 solution

Please use something which was proven to work well: SendInput:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

You do it using P/Invoke, which is already done for you, here: http://www.pinvoke.net/default.aspx/user32.sendinput[^].

Note that keybd_event has been superseded with SendInput.

As to SendMessage, it can also be used but has a number of limitations. First of all, it can be used on a windowed control only. So, WPF would be out of question (note that you did not indicate the UI library you want to use, which is bad; it' a good idea to always indicate it, as well as your application type). SendInput is preferable, because it simulates the input on lower level, as if it was called by the input device driver, so it can be used universally.

Finally, I would like to warn against the use of input simulation. It can be important in some specific cases: playing keyboard (mouse) macro, development of programs testing UI, something like that. If you simply try to use it for UI development, it would be a big abuse.

—SA
 
Share this answer
 
Comments
Rebecca1995 29-Sep-14 6:39am    
@sergey alexandrovich kryukov:- 5 on 5 thanks!
Sergey Alexandrovich Kryukov 29-Sep-14 9:51am    
You are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900