PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Differentiating between F4 and ALT-F4-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Differentiating between F4 and ALT-F4

  1. #1

    Thread Starter
    Member
    Join Date
    May 2018
    Posts
    37

    Resolved [RESOLVED] Differentiating between F4 and ALT-F4

    I'm doing some work on an app where the user can click a button or press a function key to achieve the same thing (the user wanted a keyboard interface).

    This works by putting 'if keycode = vbkeyF1 then' type statements in the form_keydown event and was working fine.

    However, now I've assigned a function to F4 you can no longer use ALT-F4 to close the program as it just triggers the F4 event and doesn't decode the ALT, which will annoy users used to common key combinations.

    Since there is no 'vbKeyAlt', is there any way of differentiating between F4 and ALT-F4?

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    725

    Re: Differentiating between F4 and ALT-F4

    Quote Originally Posted by chris223b View Post
    I'm doing some work on an app where the user can click a button or press a function key to achieve the same thing (the user wanted a keyboard interface).

    This works by putting 'if keycode = vbkeyF1 then' type statements in the form_keydown event and was working fine.

    However, now I've assigned a function to F4 you can no longer use ALT-F4 to close the program as it just triggers the F4 event and doesn't decode the ALT, which will annoy users used to common key combinations.

    Since there is no 'vbKeyAlt', is there any way of differentiating between F4 and ALT-F4?
    Not sure what you're trying to do. However to check whether the alt key is being/has been pressed use the following condition in If statements:
    Code:
    (Shift And vbAltMask) = vbAltMask
    This only works in KeyDown/KeyUp events. You may also want to check out the QueryUnloadConstants enumeration and QueryUnload event. The easiest way to do so is by using the Object Explorer. (Press F2 in Visual Basic's main window.)
    Last edited by Peter Swinkels; Jul 27th, 2019 at 03:49 PM. Reason: typo

  3. #3
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,554

    Re: Differentiating between F4 and ALT-F4

    1. Instead of if keycode = vbkeyF1 try using Select Case KeyCode ... Case vbKeyF1 ... Case vbKeyF4 ... End Select

    2. Instead of testing KeyCode only try testing KeyCode + Shift * &H1000 for exact values like this

    thinBasic Code:
    1. '--- in Form_KeyDown
    2.     Select Case KeyCode + Shift * &H1000&
    3.     Case vbKeyEscape
    4.         ...
    5.         KeyCode = 0: Shift = 0
    6.     Case vbKeyC + vbCtrlMask * &H1000&, vbKeyInsert + vbCtrlMask * &H1000&
    7.         ...
    8.     End Select
    Notice how Ctrl+C and Ctrl+Insert will both be handled by the same code.

    Notice how on ESC any further processing (bubbling) of the KeyDown event is stopped by assigning zero to these in/out event params.

    Notice how your original F4 and Alt+F4 has to be switched on completely different values from the KeyCode + Shift * &H1000 expression so you can either handle them separately or both by the same code -- whichever you want.

    cheers,
    </wqw>

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,543

    Re: Differentiating between F4 and ALT-F4

    You can save yourself a ton of grief by avoiding playing reindeer games just to create a non-standard access key mechanism like this.

    See the documentation on the Caption property for an easy, painless, and standards-compliant way to assign access keys to controls. These also work with Accessibility features in Windows, unlike "clever" schemes that try to sniff keystrokes. That could save your company potential employee discrimination lawsuits in many jurisdictions.

    You can use the Caption property to assign an access key to a control. In the caption, include an ampersand (&) immediately preceding the character you want to designate as an access key. The character is underlined. Press the ALT key plus the underlined character to move the focus to that control

    To include an ampersand in a caption without creating an access key, include two ampersands (&&). A single ampersand is displayed in the caption and no characters are underlined.

  5. #5
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,841

    Re: Differentiating between F4 and ALT-F4

    Quote Originally Posted by dilettante View Post
    That could save your company potential employee discrimination lawsuits in many jurisdictions.
    What?

  6. #6

  7. #7

    Thread Starter
    Member
    Join Date
    May 2018
    Posts
    37

    Re: Differentiating between F4 and ALT-F4

    Quote Originally Posted by wqweto View Post
    1. Instead of if keycode = vbkeyF1 try using Select Case KeyCode ... Case vbKeyF1 ... Case vbKeyF4 ... End Select

    2. Instead of testing KeyCode only try testing KeyCode + Shift * &H1000 for exact values like this

    thinBasic Code:
    1. '--- in Form_KeyDown
    2.     Select Case KeyCode + Shift * &H1000&
    3.     Case vbKeyEscape
    4.         ...
    5.         KeyCode = 0: Shift = 0
    6.     Case vbKeyC + vbCtrlMask * &H1000&, vbKeyInsert + vbCtrlMask * &H1000&
    7.         ...
    8.     End Select
    Notice how Ctrl+C and Ctrl+Insert will both be handled by the same code.

    Notice how on ESC any further processing (bubbling) of the KeyDown event is stopped by assigning zero to these in/out event params.

    Notice how your original F4 and Alt+F4 has to be switched on completely different values from the KeyCode + Shift * &H1000 expression so you can either handle them separately or both by the same code -- whichever you want.

    cheers,
    </wqw>
    This works perfectly! Thank you!

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,543

    Re: [RESOLVED] Differentiating between F4 and ALT-F4

    The issues involve screen-readers and other accessibility aids.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width