Module Key Event Handler

by Ruben 3/18/2008 10:46:00 AM

Module modKeys are to simplify the KeyPress event validation in textbox control like numeric, alpha, alphanumeric, selected character only, tab event etc. See sample below how to use this class module.

Private Sub tbInput_KeyPress(ByVal sender As Object, ByVal e As _
        system.Windows.Forms.KeyPressEventArgs) Handles tbInput1.KeyPress
  Select Case sTextBox
     '//-SELECTED CHARACTER
     Case "textbox1"
          e.KeyChar = ChrW(modKeys.Keyed(AscW(e.KeyChar), EnumCharType.vbOnListOnly, _
                      EnumKeyExec.vbEnterBackSpace, True, "[RrUuBbEeNn]"))
     '//-ALPHA ONLY
     Case "textbox2"
          e.KeyChar = ChrW(modKeys.Keyed(AscW(e.KeyChar), EnumCharType.vbAlphaOnly, _
                      EnumKeyExec.vbEnterBackSpace, True, " "))
     '//-NUMERIC ONLY
     Case "textbox3"
          e.KeyChar = ChrW(modKeys.Keyed(AscW(e.KeyChar), EnumCharType.vbNumericOnly, _
                      EnumKeyExec.vbEnterBackSpace))
     '//-ALPHANUMERIC & especial character
     Case "textbox4"
          e.KeyChar = ChrW(modKeys.Keyed(AscW(e.KeyChar), EnumCharType.vbAlphanumeric, _
                       EnumKeyExec.vbEnterBackSpace, True, "[- &*'()/#+%:$@]"))
    End Select

    Select Case AscW(e.KeyChar)
        Case System.Windows.Forms.Keys.Return
             AutoTab
    end select
End Sub

Module modKeys
  Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
         ByVal bScan As Byte, ByVal dwFlags As Integer, _
         ByVal dwExtraInfo As Integer)
  Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
         (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer

  Public Const KEYEVENTF_KEYDOWN As Short = &H0S
  Public Const KEYEVENTF_KEYUP As Short = &H2S

  Public Enum EnumCharType
     vbAlphaOnly = 0    'type for Characters only
     vbNumericOnly = 1  'type for numeric only
     vbAlphanumeric = 2 'Allow Alphanumeric
     vbOnListOnly = 3
  End Enum

  Public Enum EnumKeyExec
    vbEnterOnly = 0      'this would only allow enter key
    vbBackSpaceOnly = 1  'allow backspace only
    vbSpaceOnly = 2      'allow space only
    vbEnterBackSpace = 3 'allow both backspace and enter
    vbEnterSpace = 4     'allow enter and space
    vbSpaceBackSpace = 5 'allow space and backspace
    vbDefaultKeys = 6    'allow space,enter and backspace
  End Enum

  ''' this function will changed the value passed to keyascii
  ''' and will follow the options set in the parameters
  Function Keyed(ByVal Keyascii As Short, Optional ByVal AsciiType As _
      EnumCharType = EnumCharType.vbAlphanumeric, _
      Optional ByVal KeyExec As EnumKeyExec = EnumKeyExec.vbDefaultKeys, _
      Optional ByVal AllUpperCase As Boolean = False, _
      Optional ByVal OtherChar As String = "", Optional _
      ByVal AllowInverted As Boolean = False) As Short
  Try
     Select Case Keyascii    'trap ascii code
       Case System.Windows.Forms.Keys.Return  'allow return
           If KeyExec = EnumKeyExec.vbEnterBackSpace Or _
              KeyExec = EnumKeyExec.vbEnterOnly Or _
              KeyExec = EnumKeyExec.vbEnterSpace Or _
              KeyExec = EnumKeyExec.vbDefaultKeys Then
                Keyed = System.Windows.Forms.Keys.Return
           End If
       Case System.Windows.Forms.Keys.Space   'allow space
           If KeyExec = EnumKeyExec.vbEnterSpace Or _
              KeyExec = EnumKeyExec.vbSpaceBackSpace Or _
              KeyExec = EnumKeyExec.vbSpaceOnly Or _
              KeyExec = EnumKeyExec.vbDefaultKeys Then
                Keyed = System.Windows.Forms.Keys.Space
           End If
      Case System.Windows.Forms.Keys.Back    'allow backspace
           If KeyExec = EnumKeyExec.vbBackSpaceOnly Or _
              KeyExec = EnumKeyExec.vbEnterBackSpace Or _
              KeyExec = EnumKeyExec.vbSpaceBackSpace Or _
              KeyExec = EnumKeyExec.vbDefaultKeys Then
                Keyed = System.Windows.Forms.Keys.Back
           End If
    End Select

    Select Case AsciiType 'get option
      Case EnumCharType.vbNumericOnly 'cancel all inputs aside from numbers
           If Chr(Keyascii) Like "#" Then
              Keyed = Keyascii
           End If
      Case EnumCharType.vbAlphaOnly 'allow only characters
           If Chr(Keyascii) Like "[A-Za-z]" Then
              Keyed = Keyascii
           End If
      Case EnumCharType.vbAlphanumeric 'Numbers and Characters are accepted
           If Chr(Keyascii) Like "[A-Za-z0-9]" Then
              Keyed = Keyascii
           End If
      Case EnumCharType.vbOnListOnly
           If Chr(Keyascii) Like OtherChar Then
              Keyed = Keyascii 'return the char
           End If
    End Select

    'if there are allowable chars that are specified
    If OtherChar <> vbNullString Then
      'use pattern matching rules
      If Chr(Keyascii) Like OtherChar Then
         Keyed = Keyascii 'return the char
      End If
    End If

    'i separated this option so not to conflict with others
    If AllUpperCase = True And AsciiType <> EnumCharType.vbNumericOnly Then
       If Chr(Keyed) Like "[a-z]" Then 'if lower case
          Keyed = Asc(UCase(Chr(Keyed))) 'convert to upper case
       End If
    End If

    'if inverted option is true inverted letters to numbers
     If Chr(Keyascii) Like "[UuIiOoJjKkLlMm,><./]" Then
        If AllowInverted Then
           Select Case Keyascii
              Case Asc("U"), Asc("u")
                 Keyed = System.Windows.Forms.Keys.D1
              Case Asc("I"), Asc("i")
                 Keyed = Asc("2")
              Case Asc("O"), Asc("o")
                 Keyed = Asc("3")
              Case Asc("J"), Asc("j")
                 Keyed = Asc("4")
              Case Asc("K"), Asc("k")
                 Keyed = Asc("5")
              Case Asc("L"), Asc("l")
                 Keyed = Asc("6")
              Case Asc("M"), Asc("m")
                 Keyed = Asc("7")
              Case Asc(",")
                 Keyed = Asc("8")
              Case Asc(".")
                 Keyed = Asc("9")
              Case Asc("/")
                 Keyed = Asc("0")
           End Select
        End If
     End If

     Catch ex As Exception
        MessageBox.Show(ex.ToString, "ModKeys", MessageBoxButtons.OK, MessageBoxIcon.Information)
     End Try
  End Function

  Public Sub AutoTab()
     keybd_event(System.Windows.Forms.Keys.Tab, _
                 MapVirtualKey(System.Windows.Forms.Keys.Tab, 0), _
                 KEYEVENTF_KEYDOWN, 0)
     keybd_event(System.Windows.Forms.Keys.Tab, _
                 MapVirtualKey(System.Windows.Forms.Keys.Tab, 0), _
                 KEYEVENTF_KEYUP, 0)
  End Sub

  Public Sub AutoShiftTab()
     keybd_event(System.Windows.Forms.Keys.ShiftKey, _
                 MapVirtualKey(System.Windows.Forms.Keys.ShiftKey, 0), _
                 KEYEVENTF_KEYDOWN, 0)
     keybd_event(System.Windows.Forms.Keys.Tab, _
                 MapVirtualKey(System.Windows.Forms.Keys.Tab, 0), _
                 KEYEVENTF_KEYDOWN, 0)
     keybd_event(System.Windows.Forms.Keys.ShiftKey, _
                 MapVirtualKey(System.Windows.Forms.Keys.ShiftKey, 0), _
                 KEYEVENTF_KEYUP, 0)
  End Sub
End Module

FREE PDF BOOK DOWNLOAD

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

9/7/2008 5:27:35 AM

Recieved Updates



Enter your email address:

Delivered by FeedBurner

About the author

Name of author RUBEN CORRAL
System Developer in outSourcing company for almost 8 years. I built this blogs just for fun, sharing idea's, contribute a piece of code, especially to newbie programmers.

E-mail me Send mail

Calendar

<<  September 2008  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal point of view. Sample source codes are free to modify or enhance for your own satisfaction.

Sign in

All brand names, logos and trademarks in this site are property of their respective owners.