Results 1 to 5 of 5

Thread: Is it possible to prevent another keypress event after one key is already pressed?

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Is it possible to prevent another keypress event after one key is already pressed?

    I am developing a VB.NET data entry application and some of the data entry clerks are very fast when it comes to typing letters on keyboard.
    One of them constantly presses two buttons one after the other in like 100 milliseconds(well not sure if this time frame is correct but it happens very fast).
    This means detecting two OnKeyUp() events once the two buttons are released, one after the other in again several milliseconds time-frame.

    How can I prevent the second button KeyPress and then KeyUp events being detected by the application?

    Yes there is the flag event.Handled = False but how would you make it for buttons being pressed in a sequence in a short time-frame?

    Could I do something on keyboard level?

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Is it possible to prevent another keypress event after one key is already pressed

    Without more information, I would wonder why your program can't cope with fast typing. Even a pro typist is very slow compared to the speed of your CPU. Think how many instructions can be executed in a couple of milliseconds. Can you show the code causing the problem and explain what it is you need to do in the KeyUp events?

  3. #3

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Is it possible to prevent another keypress event after one key is already pressed

    There is not a bug. The program works as expected given that at some point the buttons are being pressed and released one after the other.
    The application was old and badly made. Had to rework some stuff. I am just finding solutions to the problem of jumping two text fields when you press two buttons one after the other and release them at once.

    Here is a sample code:

    Given these three text fields, tf1, tf2 and tf3. tf1 selection length is 2 as is tf2 but tf3 is of length 3 letters.
    On load, tf2.Text is set to two zeroes like so

    Code:
    public sub OnFormLoad()
        tf2.Text = "00"
        tf2.Focus()
    end sub
    I have these events on the three textfields

    tf1 code:

    Code:
    ' dont mind me, I cannot recall the event parameters list
    public sub tf1_GotFocus(e)
         tf1.SelectAll()
    end sub
    
    public sub tf1_KeyPress(e)
         if e.KeyCode = keys.Enter then
              if Len(tf1.Text) = 2 then
                   runChecksForTF1AndJumpToTF2()
              end if
         else
              e.Handled = false
         end if
    end sub
    
    public sub tf1_KeyUp(e)
        if e.Keycode = keys.Enter or e.Keycode = keys.Right then
                e.Handled = False
        else 
                if Len(tf1.Text) = 2 then
                   runChecksForTF1AndJumpToTF2()
                end if
        end if
    end sub
    tf2 code:

    Code:
    ' dont mind me, I cannot recall the event parameters list
    public sub tf2_GotFocus(e)
         tf2.SelectAll()
    end sub
    
    public sub tf2_KeyPress(e)
         if e.KeyCode = keys.Enter then
              if Len(tf2.Text) = 2 then
                   runChecksForTF2AndJumpToTF3()
              end if
         else
              e.Handled = false
         end if
    end sub
    
    public sub tf2_KeyUp(e)
        if e.Keycode = keys.Enter or e.Keycode = keys.Right then
                e.Handled = False
        else 
                if Len(tf2.Text) = 2 then
                   runChecksForTF2AndJumpToTF3()
                end if
        end if
    end sub
    tf3 code:

    Code:
    ' dont mind me, I cannot recall the event parameters list
    public sub tf3_GotFocus(e)
         tf3.SelectAll()
    end sub
    
    public sub tf3_KeyPress(e)
         if e.KeyCode = keys.Enter then
              if Len(tf3.Text) = 3 then
                   runChecksForTF3AndJumpToTF4()
              end if
         else
              e.Handled = false
         end if
    end sub
    
    public sub tf3_KeyUp(e)
        if e.Keycode = keys.Enter or e.Keycode = keys.Right then
                e.Handled = False
        else 
                if Len(tf3.Text) = 3 then
                   runChecksForTF3AndJumpToTF4()
                end if
        end if
    end sub
    Use case:

    Typing to field tf1 two letters, pressing one after the other in short time frame. So say, type "0" and after several milliseconds you press "1".

    Releasing the first key(pressed "0"), the event tf1_KeyUp() is triggered and since the field contains 2 letters "01". "01" represents a valid value so runChecksForTF1AndJumpToTF2() function moves the focus to the field tf2.

    Releasing the second key (pressed "1"), the event tf2_KeyUp() is triggered. tf2 already contains two letters "00" and "00" is a valid value so runChecksForTF2AndJumpToTF3() runs without problem and moves the focus to tf3.

    Given this use case you have just typed two letters and you are already on the third text field which is logical.
    I just want to see what can I do before changing overall how this application works. I think also that setting zeroes to tf2 can be done at different time, not at load time.
    Last edited by kutlesh; Jun 15th, 2019 at 10:11 AM.

  4. #4
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Is it possible to prevent another keypress event after one key is already pressed

    looks like vb6...
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  5. #5

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: Is it possible to prevent another keypress event after one key is already pressed

    VB6 and VB.NET events are alike. I just removed the nasty OnValidate() aka LostFocus() events which printed an error message window when wrong value was entered. Guess what, you couldn't move your mouse cursor or the focus from the selected field until you enter appropriate value. You couldn't even close the fu**ing application window until you enter appropriate value.

    Ok, so no no then?

    Better ways than this in WinForms? I mean better way to develop ordinary CRUD data entry application in WinForms with VB.NET?
    Any tutorials on the topic?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width