I am trying to develop an on screen keyboaard for a UMPC. I have everything built and for each button click i have code for my sendkeys, however, i have not a clue where to start when it comes to focusing the application. I need to know how to after a button press on my form, return focus to the previously focused window in the correct area so that it can then send a keypress. Any And All help is greatly appreciated as i do now know at all what to do for this. Thanks
~Matt

Hi Matt,

I just faced the same issue and finally solved it. I was amazed I could not find vb.net help on the subject. Anyhow...

I declared a public variable in the highest level form (mine was a multi-document interface), and declared it as a control.

Public LastFocus as Control

Then in every click event for objects my keyboard might be used to enter data into (text boxes etc.), I put a line in saving the event sender as the new LastFocus.

LastFocus=eventsender (or just sender whichever)

After my keyboard got the focus and sent the key data to the system, I put in a line of code to return focus to the last clicked control:

LastFocus.focus

It worked like a champ. If you have trouble, you can contact me <<Email Snipped>>

Edited 6 Years Ago by __avd: Email Snipped.

Hi Matt,

I just faced the same issue and finally solved it. I was amazed I could not find vb.net help on the subject. Anyhow...

I declared a public variable in the highest level form (mine was a multi-document interface), and declared it as a control.

Public LastFocus as Control

Then in every click event for objects my keyboard might be used to enter data into (text boxes etc.), I put a line in saving the event sender as the new LastFocus.

LastFocus=eventsender (or just sender whichever)

After my keyboard got the focus and sent the key data to the system, I put in a line of code to return focus to the last clicked control:

LastFocus.focus

It worked like a champ. If you have trouble, you can contact me <<Email Snipped>>

Private Sub kbdA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kbdEscape.Click
        LastFocus = sender
        MessageBox.Show(sender.ToString)
        SendKeys.send(keys.a)
        LastFocus.Focus()
End Sub

this is what i have, but my messagebox keeps returning the string of the picture box in which the picture of the key is that is clicked. what in the Private Sub decelerations line do i need to put in for sender?
Thanks
~Matt C

Edited 6 Years Ago by matt.clark.228: n/a

Sorry Matt, I did not make myself clear. Each control that might receive keys from the keyboard must set the variable LastFocus to point to itself so that the keyboard routine knows where to redirect the focus to. For example:

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
        LastFocus = sender
        ShowKeyboard()
    End Sub

In my case I use a keyboard that I designed as a COM object under VB-6. The line "showkeyboard" simply makes it visible if it is hidden. It performs the sendkeys internally. In your case, you should probably put the LastFocus.Focus statement before your SendKeys statement in your keyboard routine. Here is how mine looks.

Private Sub KeyBoard1_KeyClick(ByVal eventSender As System.Object, ByVal eventArgs As AxHTControls.__KeyBoard_KeyClickEvent) Handles KeyBoard1.KeyClick
        On Error Resume Next
        LastFocus.Focus()
        Playwave("click.wav") 'Note this is a routine that produces a key click sound.
End Sub

i am still not understanding this. the way you have explained it, your object sender will still return a value of textbox1.click, instead of the name of the previously focused item so that the focused can then be returned back to it...

The click event for the Textbox simply saves the "sender" as a control variable. The sender is TextBox1 in this case. TextBox1.click is an event, not an object.

Think of it as it happens. You want to enter something into TextBox1, so you click on it as you would for any application. With a touch screen you touch it, which is exactly the same as a click event.

When you do, the click event subroutine saves a pointer to TextBox1. It also puts focus on the TextBox as you can tell from the cursor appearing there.

Then you touch a key on your keyboard. You want this to put characters into the TextBox1, but the act of touching the keyboard changes the focus away from TextBox1 and to the keyboard. You can tell, because the cursor disappears out of the TextBox.

So the keyboard tries to send characters to itself. There is no command I know of to simply send focus back to the previous control. But if the keyboard can look up which control last had focus before it got it (because we saved it in LastFocus), then it can restore it to that control (in this case TextBox1). Every control that you might pipe keyboard data into will have to have such a click event.

In my case, I use the click event on the textbox to also make sure the keyboard is showing. VB6 was much easier in that you could set a control so that it would not take focus in the first place.

Try this (assuming your keyboard code is correct):

Public LastFocus as Control

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
      LastFocus = sender
 End Sub

Private Sub kbdA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kbdEscape.Click
        MessageBox.Show(sender.ToString)
        LastFocus.Focus()
        SendKeys.send(keys.a)

End Sub

For multiple text boxes you might use a single event like this:

Private Sub TextBoxes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click,TextBox2.Click,TextBox3.Click
      LastFocus = sender
 End Sub

Edited 6 Years Ago by Hollandtech: n/a

i still do not understand this. when i do this, sender is whatever i click, could someone please post their code so i can see what i am doing wrong. thanks, Matt

Yes Matt, that is exactly correct. You want LastFocus to be whatever you last clicked (besides the keyboard). Then when you click the keyboard and it gets the focus, the keyboard can return focus to the previous object before sending the system keys.

Sorry, I don't know how to explain it more clearly. It works for me.

Yes Matt, that is exactly correct. You want LastFocus to be whatever you last clicked (besides the keyboard). Then when you click the keyboard and it gets the focus, the keyboard can return focus to the previous object before sending the system keys.

Sorry, I don't know how to explain it more clearly. It works for me.

The problem i have is that the sender that it returns is the button/picturebox/textbox that i click on.
i.e.
i click on a key on my osk

Private Sub keyA_Click (Byval sender as system object) Handles kbdKeyA.click

 MessageBox.show(sender.tostring)

End Sub

Message Box Returns:
System.Windows.Forms.PictureBox, SizeMode: AutoSize

The problem i have is that the sender that it returns is the button/picturebox/textbox that i click on.
i.e.
i click on a key on my osk

Private Sub keyA_Click (Byval sender as system object) Handles kbdKeyA.click

 MessageBox.show(sender.tostring)

End Sub

Message Box Returns:
System.Windows.Forms.PictureBox, SizeMode: AutoSize

Each text box or other object where text from the keyboard is to go should capture SENDER and save it as the global object LastFocus. You ignore SENDER in your key code (because yes it will be the key you just pressed). In the key code set the focus back to the LastFocus and then send the system keys.

I did find that the variable name LastFocus could cause some system confusion, so I am using LastSelected.

Here is a simple application. It consists of a keyboard with only three keys made from labels. In their properties window, I initialize the text of the three labels to be "A", "B", and "C" respectively. I set the font to be 36 point to make them nice and large.

It also has three text boxes where key strokes can go. They are Textbox1, Textbox2, and Textbox3.

One clicks on one of the textboxes, and then enters any series of A, B or C into that textbox using the three keys.

Public Class Form1

    Public LastSelected As Object 'This is the object that will receive any keys.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LastSelected = Me.TextBox1 'Initialize LastSelected to point at textbox1.
    End Sub

    Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click, TextBox2.Click, TextBox3.Click
        LastSelected = sender 'Remember this textbox as LastSelected
    End Sub


    Private Sub LabelA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelA.Click, LabelB.Click, LabelC.Click

        LastSelected.focus() 'Set the focus to lastselected of the textboxes.
        SendKeys.Send(sender.text) 'Extract the single letter from the label clicked (A,B or C) and send it as if it came from the console.
        Beep() 'Beep to acknowledge key press.

    End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.