Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Aug 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with KeyPress event and KeyAscii (access 97)

    I expected KeyAscii to be 13 when the Return key was pressed. All the normal keys give the expected values, but Return gives nothing. This works in VB. Is there some wierd thing in Access I need to know about? How then do I test for a Return key being pressed if not with the standard,
    If KeyAscii = vbKeyReturn Then ( or , If KeyAscii = 13 )
    do stuff
    End If

    Brad

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Help with KeyPress event and KeyAscii (access 97)

    In the keydown event, I test for :

    Keycode = vbKeyReturn
    Regards
    John



  3. #3
    Lounger
    Join Date
    Aug 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with KeyPress event and KeyAscii (access 97)

    Ok, granted that works. But how is the Keydown event any different from the Keypress event? Both variables are set as Integers. Why will KeyDown recognize the Return key, but KeyPress will not? <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

    Or is there an API somewhere I can read?
    Brad

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Help with KeyPress event and KeyAscii (access 97)

    Look up KeyPress and KeyDown/KeyUp in the online Help. The Access 97 help has a good explanation with examples. Basically:
    <UL><LI>If you want to know if the user typed a character, for instance a "D", use the KeyPress event.
    <LI>If you want to handle special keys like arrow keys, function keys, and combinations of Ctrl, Alt and/or Shift with other keys, use the KeyDown or KeyUp events.[/list]The KeyPress event occurs when the user presses and releases an ANSI key, that is a key corresponding to an ANSI character (letters, digits, punctuation, symbols, ...)

    Use KeyDown and KeyUp event procedures to handle any keystroke not recognized by KeyPress, such as function keys, editing keys, navigation keys, and any combinations of these with keyboard modifiers. Unlike the KeyDown and KeyUp events, KeyPress doesn't indicate the physical state of the keyboard; instead, it passes a character.

    KeyPress interprets the uppercase and lowercase of each character as separate key codes and, therefore, as two separate characters. KeyDown and KeyUp interpret the uppercase and lowercase of each character by means of two arguments: KeyCode, which indicates the physical key (thus returning A and a as the same key), and Shift, which indicates the state of the Shift, Alt and Control keys. Acces provides constants acShiftMask, acCtrlMask and acAltMask to check. For instance, to check for Ctrl+E, you would use something like

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyE Then
    If (Shift And acCtrlMask) > 0 Then
    ...
    End If
    End If
    End Sub

  5. #5
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Help with KeyPress event and KeyAscii (access 97)

    I have just been reading the Help about this in Access 97 and 2000, and it says :
    "A KeyPress event can involve any printable keyboard character, the CTRL key combined with a character from the standard alphabet or a special character, and the ENTER or BACKSPACE key. "

    Is it just wrong to include Enter in this list?
    Regards
    John



  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Help with KeyPress event and KeyAscii (access 97)

    This is slightly complicated.

    (1) The KeyPress event of a text box doesn't "see" Enter or Tab, because Access handles these keystrokes (by setting the focus to the next control) and doesn't pass them to the text box.

    (2) The KeyDown event of a text box *does* "see" Enter or Tab.

    (3) If you set the KeyPreview property of a form to Yes, the KeyPress event of the form *does* "see" Enter or Tab.

Posting Permissions

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