Results 1 to 5 of 5

Thread: Turning a Boolean FALSE if the right KeyData isn't pressed.

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    36

    Turning a Boolean FALSE if the right KeyData isn't pressed.

    Hey! Me again! A week or so ago, you folks really taught me how to use the KeyData event. I adopted some of the coding to make my new code so much better. A few days ago, I thought I figured out how to run my little game. You see, you have two buttons. Comment and Uncomment. You Press Control + K and then after that, you press Control + (C or U) to activate the buttons. And I thought it worked! But my buddy figured out how to break it! The thing is, I need to figure out a way to make IsCtrlK (Look below) False if any key that isn't Control + C or U is pressed right after it. What would you suggest I look into using?

    Here is the code.

    Code:
        Dim IsCtrlK As Boolean
            Select Case e.KeyData
                Case Keys.Control Or Keys.K
                    IsCtrlK = True
            End Select
            If IsCtrlK = True Then
                Select Case e.KeyData
                    Case Keys.Control Or Keys.C
                        ToolstripButtonCommentText.PerformClick()
                        IsCtrlK = False
                    Case Keys.Control Or Keys.U
                        ToolstripButtonUncommentText.PerformClick()
                        IsCtrlK = False
                End Select
            End If
    Last edited by kleinma; Aug 2nd, 2010 at 12:33 PM. Reason: formatted code

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Turning a Boolean FALSE if the right KeyData isn't pressed.

    Could you wrap the code in a code block? It's really hard to read & understand like that.

    In short, you'll want to check for just the ctrl key (and if so) then check for K, C or U inside the If block.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    36

    Re: Turning a Boolean FALSE if the right KeyData isn't pressed.

    Ah- Sorry about that.

    What I'm wanting to do is press Control + K and then having to press Control + (C or U) to activate the button click event. The problem is, with the current code, if I press Control + K and then... A bunch of other keys... and THEN Control + (C or U), it still counts. I want it to be Control + K then Control + (C or U) or no dice. If another key is pressed before the Control + (C or U), then I want to it not to count as the correct combination.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Turning a Boolean FALSE if the right KeyData isn't pressed.

    how about something like this?

    Code:
            Static IsCtrlK As Boolean
    
            'IF CTRL+K HAS NOT BEEN PRESSED YET, CHECK FOR IT SINCE IT
            'IS THE FIRST THING THAT NEEDS TO HAPPEN
            If Not IsCtrlK Then
                IsCtrlK = (Keys.Control Or Keys.K) = e.KeyData
            Else
                'SINCE CTRL+K WAS PREVIOUSLT PRESSED
                'IT SHOULD BE SET TO FALSE NOW UNDER ALL SCENARIOS
                IsCtrlK = False
    
                'CHECK FOR CTRL+C OR CTRL+U
                Select Case e.KeyData
                    Case Keys.Control Or Keys.C
                        ToolstripButtonCommentText.PerformClick()
                    Case Keys.Control Or Keys.U
                        ToolstripButtonUncommentText.PerformClick()
                End Select
            End If

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Turning a Boolean FALSE if the right KeyData isn't pressed.

    Here's my take:
    vb.net Code:
    1. Static isInCtrlKChord As Boolean = False
    2.  
    3. Select Case e.KeyData
    4.     Case Keys.Control Or Keys.K
    5.         isInCtrlKChord = Not isInCtrlKChord
    6.     Case Keys.Control Or Keys.C
    7.         'Do whatever.
    8.         isInCtrlKChord = False
    9.     Case Keys.Control Or Keys.U
    10.         'Do whatever.
    11.         isInCtrlKChord = False
    12.     Case Else
    13.         isInCtrlKChord = False
    14. End Select
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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