none
Block keyboard input RRS feed

  • Question

  • Hello all, im using this code:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.D1
                    Label1.ForeColor = Color.Green
                    ...code
    
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True

    To get keypress actions.

    In this case if i press the key "1" my label1.forecolor will be green and then execute the code.

    I want to know how can i disable the key "1" till the end of the code cause if i press "1" twice i can got errors since the code is executed 2 times.

    Thanks

    Thursday, July 27, 2017 11:41 AM

Answers

  • leshay works great now but can we remove keypress to set duplicate to false?

    Maybe:

        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Static Duplicate As Boolean = False
            Select Case keyData
                Case Keys.D1
                    If Not Duplicate Then
                        Label1.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
    		    ' reset all
    		Duplicate = False
                    Label1.ForeColor = Color.Black
                    Label2.ForeColor = Color.Black
                    End If
    
                Case Keys.D2
                    If Not Duplicate Then
                        Label2.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
                    ' reset all
    		Duplicate = False
                    Label1.ForeColor = Color.Black
                    Label2.ForeColor = Color.Black
                    End If
    
    
    
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True
        End Function

    • Edited by extream87 Thursday, July 27, 2017 1:05 PM
    • Marked as answer by extream87 Friday, July 28, 2017 9:38 PM
    Thursday, July 27, 2017 1:04 PM

All replies

  • Hello all, im using this code:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.D1
                    Label1.ForeColor = Color.Green
                    ...code
    
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True

    To get keypress actions.

    In this case if i press the key "1" my label1.forecolor will be green and then execute the code.

    I want to know how can i disable the key "1" till the end of the code cause if i press "1" twice i can got errors since the code is executed 2 times.

    Thanks

    Hi

    Maybe something like this. I use a List to allow for multiple Key combinations, but if you only want to check for one Key then a simple variable would do it.

    This swallows all repeated '1' key presses but only once reaches the ' ..code'.

        Dim Duplicate As New List(Of Keys)
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Select Case keyData
                Case Keys.D1
                    If Not Duplicate.Contains(keyData) Then
                        Label1.ForeColor = Color.Green
                        Duplicate.Add(keyData)
                        '  ...code
                        Duplicate.Clear()
                    End If
                    Return True
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True
        End Function


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 27, 2017 12:06 PM
    • Marked as answer by extream87 Thursday, July 27, 2017 12:14 PM
    • Unmarked as answer by extream87 Thursday, July 27, 2017 12:16 PM
    Thursday, July 27, 2017 12:05 PM
  • Thanks leshay but if i add one more case D1 not prevent D2 to be pressed.

        Dim Duplicate As New List(Of Keys)
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Select Case keyData
                Case Keys.D1
                    If Not Duplicate.Contains(keyData) Then
                        Label1.ForeColor = Color.Green
                        Duplicate.Add(keyData)
                        '  ...code
                        Duplicate.Clear()
                    End If
                    Return True
    
    	    Case Keys.D2
                    If Not Duplicate.Contains(keyData) Then
                        Label2.ForeColor = Color.Green
                        Duplicate.Add(keyData)
                        '  ...code
                        Duplicate.Clear()
                    End If
                    Return True
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True
        End Function
    I can solve this with a boolean but i dont know if is the right method...


    • Edited by extream87 Thursday, July 27, 2017 12:28 PM
    Thursday, July 27, 2017 12:15 PM
  • Thanks leshay but if i add one more case D1 not prevent D2 to be pressed.

        Dim Duplicate As New List(Of Keys)
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Select Case keyData
                Case Keys.D1
                    If Not Duplicate.Contains(keyData) Then
                        Label1.ForeColor = Color.Green
                        Duplicate.Add(keyData)
                        '  ...code
                        Duplicate.Clear()
                    End If
                    Return True
    
    	    Case Keys.D2
                    If Not Duplicate.Contains(keyData) Then
                        Label2.ForeColor = Color.Green
                        Duplicate.Add(keyData)
                        '  ...code
                        Duplicate.Clear()
                    End If
                    Return True
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True
        End Function

    Hi

    How about this then ........

    NOTE: the Escape key is used here to Reset everything (for testing)

        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Static Duplicate As Boolean = False
            Select Case keyData
                Case Keys.D1
                    If Not Duplicate Then
                        Label1.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
                    End If
    
                Case Keys.D2
                    If Not Duplicate Then
                        Label2.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
                    End If
    
                    ' reset all
                Case Keys.Escape
                    Duplicate = False
                    Label1.ForeColor = Color.Black
                    Label2.ForeColor = Color.Black
    
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True
        End Function


    Regards Les, Livingston, Scotland

    Thursday, July 27, 2017 12:42 PM
  • leshay works great now but can we remove keypress to set duplicate to false?

    Maybe:

        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Static Duplicate As Boolean = False
            Select Case keyData
                Case Keys.D1
                    If Not Duplicate Then
                        Label1.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
    		    ' reset all
    		Duplicate = False
                    Label1.ForeColor = Color.Black
                    Label2.ForeColor = Color.Black
                    End If
    
                Case Keys.D2
                    If Not Duplicate Then
                        Label2.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
                    ' reset all
    		Duplicate = False
                    Label1.ForeColor = Color.Black
                    Label2.ForeColor = Color.Black
                    End If
    
    
    
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True
        End Function

    • Edited by extream87 Thursday, July 27, 2017 1:05 PM
    • Marked as answer by extream87 Friday, July 28, 2017 9:38 PM
    Thursday, July 27, 2017 1:04 PM
  • leshay works great now but can we remove keypress to set duplicate to false?

    Maybe:

        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Static Duplicate As Boolean = False
            Select Case keyData
                Case Keys.D1
                    If Not Duplicate Then
                        Label1.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
    		    ' reset all
    		Duplicate = False
                    Label1.ForeColor = Color.Black
                    Label2.ForeColor = Color.Black
                    End If
    
                Case Keys.D2
                    If Not Duplicate Then
                        Label2.ForeColor = Color.Green
                        Duplicate = True
                        '  ...code
                    ' reset all
    		Duplicate = False
                    Label1.ForeColor = Color.Black
                    Label2.ForeColor = Color.Black
                    End If
    
    
    
                Case Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
            End Select
            Return True
        End Function

    Hi

    Sorry, but I don't understand what you mean.


    Regards Les, Livingston, Scotland

    Thursday, July 27, 2017 1:15 PM