Author Topic: New Keyboard - Similar to OSK  (Read 3616 times)

Phrog30

  • Guest
New Keyboard - Similar to OSK
« on: June 18, 2017, 02:19:51 PM »
Here's a keyboard I created that's similar to Microsoft's OSK.  It's just something I was playing around with.  It seems to work ok.  I created a class that I used to call the keyboard, it basically ensures only one keyboard at a time.  I'm sure there are better ways, but this is what I came up with.

Code: [Select]
Public Class Keyboard

    Public Sub Open()

        Dim openForms As Windows.Forms.FormCollection = Application.OpenForms
        For Each frm As Windows.Forms.Form In openForms
            If frm.Name = "Keyboard_v3" Then
                frm.BringToFront()
                Return
            End If
        Next
        Dim kpd As New Keyboard_v3
        kpd.Show()

    End Sub

End Class

Then to open the keyboard, say from from a textbox, use this code:

Code: [Select]
If Globals.kb Is Nothing Then
            Globals.kb = New Keyboard
        End If
        Globals.kb.Open()

I also have a "Global" class:

Code: [Select]
Public Class Globals

    Public Shared kb As Keyboard

End Class

Phrog30

  • Guest
Re: New Keyboard - Similar to OSK
« Reply #1 on: June 18, 2017, 02:29:21 PM »
I should add, one benefit of using a keyboard like this is that it will use the keypress event, so if you have code on a textbox like the following:

Code: [Select]
Private Sub Textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
        Dim allowedChars As String = "0123456789"
        If allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

It will work by not allowing characters you don't want.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: New Keyboard - Similar to OSK
« Reply #2 on: May 24, 2018, 11:12:12 PM »
Attached in this post is the modified version of this keyboard, as I thought it could be.
Also included is the modified BasicLabel which can activate this keyboard.

The attached picture shows what it looks like.
It is using the form’s title text to display the value.
Font and ForeColor can be changed as well, either use the included BasicLabel or supply your own code.
The form is set to auto scale with the font, so the bigger the font the bigger the keyboard.

It should be sufficient to add both controls as existing items to the PurchasedControls folder.

All the instructions provided by Phrog30 in the previous posts should still apply.
« Last Edit: May 26, 2018, 11:18:15 PM by Godra »

Phrog30

  • Guest
Re: New Keyboard - Similar to OSK
« Reply #3 on: May 25, 2018, 10:47:45 PM »
Just curious why you have a dedicated quit button when you also have a form close and ability to use escape?

The consensus around the office was everyone liked the alpha style keyboards better, with the textbox, so we don't use this keyboard. It was a good exercise though.

Something unrelated to this keyboard, when using the alpha keyboard, does anyone see a UI freeze when hitting the enter key?  Clicking on the enter is fine, only when using keyboard. Strange.

James

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: New Keyboard - Similar to OSK
« Reply #4 on: May 26, 2018, 12:07:55 AM »
Good point about the quit button and form close comparison.
This quit button does serve as the Esc button, which is not present and the ability to escape would then require external keyboard.

Also, because the value is displayed as the title text, "SelectAll" didn't function for it.
I didn't want to try replacing it with either "Ctrl" or "Alt" button since it didn't make much sense to me, so "Quit" was kind of quick and dirty choice. At least, all the buttons on this keyboard currently do something.
« Last Edit: May 26, 2018, 10:22:20 AM by Godra »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: New Keyboard - Similar to OSK
« Reply #5 on: May 26, 2018, 02:40:30 PM »
Attached in this post is another variant of this keyboard, this time with a TextBox to display value.

The attached picture shows what it looks like.

The PasswordChar feature is also enabled and the attached BasicLabel was updated to control it.

You can also add an icon to the form by adding that icon to the AdvancedHMIControls project Resources.
Then just look for the following line of code:

          'Me.Icon = My.Resources.AHMI32x32_4

Remove the comment and possibly change AHMI32x32_4 to the name of your icon.

The SelectAll and arrow buttons do work for the text inside the textbox but that is not visible since this keyboard doesn't seem to allow focusing (or maybe I did something wrong).

If you would like to remove the border, then change the following line from Fixed3D to None:

        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D

It will look like the attached picture but will still allow the keyboard to be moved around by clicking-holding-dragging the textbox.
« Last Edit: May 28, 2018, 04:37:38 PM by Godra »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: New Keyboard - Similar to OSK
« Reply #6 on: May 28, 2018, 04:54:20 PM »
The keyboard with textbox in my previous post was updated and replaced.

Since this keyboard is designed to send keystrokes to the active application (or control), it is easy to steal its focus so I have decided to show it as modal form (the included BasicLabel was updated for this).

This way the focus is preserved within the calling application (AHMI in this case) but if you switch to another application while the keyboard is on screen the focus will get stolen.

If you would like to call this keyboard from a textbox, here is a code example of how to do it:

Code: [Select]
    Private AlphaKeyboard As AdvancedHMIControls.Keyboard_v3
    Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter, TextBox1.MouseUp
        If AlphaKeyboard Is Nothing Then
            AlphaKeyboard = New AdvancedHMIControls.Keyboard_v3
            AlphaKeyboard.ShowDialog()
        Else
            If Not String.IsNullOrEmpty(Me.TextBox1.Text) Then
                AlphaKeyboard.Value = Me.TextBox1.Text
                AlphaKeyboard.SelectionStart = Me.TextBox1.SelectionStart
                AlphaKeyboard.SelectionLength = Me.TextBox1.SelectionLength
            End If
            AlphaKeyboard.ShowDialog()
        End If
    End Sub

The AlphaKeyboard.SelectionStart and AlphaKeyboard.SelectionLength properties were added in attempt to synchronize the keyboard's textbox with the calling textbox (for highlighted text).