Results 1 to 12 of 12

Thread: Detect shift key

  1. #1

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Angry Detect shift key

    In vb6 how do i detect whether the shift key is being pressed? I have read something about vbShiftMask but i cant seem to implement it into my program.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Detect shift key

    You can use the KeyDown property of a textbox (or other controls)

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    4.   If Shift = 1 Then Debug.Print "Shift " & Chr(KeyCode)
    5. End Sub

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Detect shift key

    You can't detect it because there is no event for it.
    It is used along with other keys like "a" to "z"...

    for example if you press shift then "a", then you can detect that shift was pressed also.

    [Edit], UPS.... nevermind, it seems that I was wrog

  4. #4

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Detect shift key

    dglienna's code worked fine.

    How would I make it detect that it is not being pressed?

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Detect shift key

    Shift will be 0 if it isn't being pressed.

  6. #6

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Detect shift key

    look at this:
    VB Code:
    1. Private Sub form_KeyDown(KeyCode As Integer, shift As Integer)
    2.   If shift = 1 Then
    3.   engupper.Visible = True
    4.     englower.Visible = False
    5.     shiftcheck = False
    6. Else
    7.     englower.Visible = True
    8.     engupper.Visible = False
    9. End If
    10. End Sub
    when I let go of shift then "engupper" just remains hidden. Any suggestions?

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Detect shift key

    Quote Originally Posted by ajames
    look at this:
    VB Code:
    1. Private Sub form_KeyDown(KeyCode As Integer, shift As Integer)
    2.   If shift = 1 Then
    3.   engupper.Visible = True
    4.     englower.Visible = False
    5.     shiftcheck = False
    6. Else
    7.     englower.Visible = True
    8.     engupper.Visible = False
    9. End If
    10. End Sub
    when I let go of shift then "engupper" just remains hidden. Any suggestions?
    I think the IF block should be in ELSE and vice-versa.
    VB Code:
    1. Private Sub form_KeyDown(KeyCode As Integer, shift As Integer)
    2. If shift = 1 Then
    3.     englower.Visible = True
    4.     engupper.Visible = False
    5. Else
    6.     engupper.Visible = True
    7.     englower.Visible = False
    8.     shiftcheck = False
    9. End If
    10. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Detect shift key

    It still refuses to switch back when I let go of shift, however, if I press another button it switches back

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Detect shift key

    I think you should use the GetAsyncKeyState API instead of form events. That's more reliable.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Detect shift key

    What are they and how do i do them?

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Detect shift key

    It's an API that returns the key state of any key at that time - whether it is pressed or not. Just search for "GetAsyncKeyState" in this forum and you would get a lot of examples.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Detect shift key

    Something like...
    VB Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    2.  
    3. Private Sub MyProcedure()
    4.     If GetAsyncKeyState(vbKeyShift) Then
    5.         'Shift key is pressed
    6.     Else
    7.         'shift is not pressed
    8.     End If
    9. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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