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

Phrog30

  • Sr. Member
  • ****
  • Posts: 250
    • View Profile
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

  • Sr. Member
  • ****
  • Posts: 250
    • View Profile
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.