Using System.forms.sendkeys.send

Posted on 2006-05-29
Medium Priority
Last Modified: 2008-01-09
Hi, I am trying to make a keyboard to be used with a touchscreen.
I tried set forcus on a textbox and use system.form.sendkeys.send("A") and it worked. According to the .NET Framework reference it says...if to enter special characters dont not show i will have to use the codes.
For e.g. {BACKSPACE} will do the backspace keystroke.

However i am not sure how to use the code. I tried doing it ...... system.form.sendkeys.send("{BACKSPACE}") but it won't work... any ideas how can i get it to work with set forcus on a textbox?

thanks alot
Question by:jaxrpc
LVL 44

Expert Comment

ID: 16783889
Hello jaxrpc,

>> with set focus on a textbox
worked for me like


that was on a buttonclick did you use an other event?

sharing what you know, learning what you don't

Author Comment

ID: 16784451
Hi thanks for replying.... what about capslock..... i tried system.windows.forms.sendkeys.send("{CAPSLOCK}") and after that system.windows.forms.sendkeys.send("A") it doesn't change "A" to "a" or it doesnt work this way?

Accepted Solution

neilprice earned 2000 total points
ID: 16784799
I found that using SendKeys with a {CAPSLOCK} and then immediately sending the rest of the keys worked.  However calling SendKeys from somewhere else in my code didn't take into account the fact I had called CAPSLOCK earlier.

To get around this you can simulate CAPSLOCK being pressed with a pinvoke from "user32.dll" of SendInput.

Details can be found at: http://www.pinvoke.net/default.aspx/user32/SendInput.html

It's a little convoluted but easy enough to make it work.

Add a new class to your project and call it "NativeMethods".  Copy and paste the code from the website above (between Public Class NativeMethods and End Class) making sure to add a reference to System.Runtime.InteropServices at the top of the class' code.  (Imports System.Runtime.InteropServices)

Then in your code you can add the "DoKeyBoard" method from that page somewhere on your form...

Private Sub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, ByVal key As Keys)
    Dim input As New NativeMethods.INPUT
    Dim ki As New NativeMethods.KEYBDINPUT
    input.dwType = NativeMethods.InputType.Keyboard
    input.ki = ki
    input.ki.wVk = Convert.ToInt16(key)
    input.ki.wScan = 0
    input.ki.time = 0
    input.ki.dwFlags = flags
    input.ki.dwExtraInfo = IntPtr.Zero
    Dim cbSize As Integer = Marshal.SizeOf(GetType(NativeMethods.INPUT))
    Dim result As Integer = NativeMethods.SendInput(1, input, cbSize)
    If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
End Sub

Then to toggle CapsLock you can call the following two lines;

        DoKeyBoard(0, Keys.CapsLock)
        DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.CapsLock)

(The two calls simulate the key being pressed and then released to switch the actual CAPSLOCK indicator light on in your system)



Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Suggested Courses
Course of the Month7 days, 23 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