On Key Press event help (1 Viewer)

GregSmith

Registered User.
Local time
Yesterday, 19:40
Joined
Feb 22, 2002
Messages
126
I am trying to use the on ket press event.
My code is simple:

Private Sub SourceRecord_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 Then MsgBox "Delete Key Pressed"
End Sub

Only thing is - it does not reconize the del key being pressed.

Any idea's on how to use the delete key?

Thanks
 

Mile-O

Back once again...
Local time
Today, 01:40
Joined
Dec 10, 2002
Messages
11,316
I thought that Delete was 8.
 

Mile-O

Back once again...
Local time
Today, 01:40
Joined
Dec 10, 2002
Messages
11,316
Na, 8 is Backspace.

Have you tried using the constant?

If KeyAcscii = vbKeyDelete Then
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:40
Joined
Aug 30, 2003
Messages
36,128
I tested with:

MsgBox KeyAscii

backspace is 8; the delete key didn't activate the keypress event (no message box). Not sure how you'd trap for it.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:40
Joined
Aug 30, 2003
Messages
36,128
Sorry Mile, I was too slow with the backspace = 8. I tested this, which also didn't work (nothing when delete pressed):
Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
  If KeyAscii = vbKeyDelete Then
    MsgBox "delete"
  Else
    MsgBox KeyAscii
  End If
End Sub
 

Mile-O

Back once again...
Local time
Today, 01:40
Joined
Dec 10, 2002
Messages
11,316
The answer is in the help files under KeyPress event. :rolleyes:

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. You can use the KeyDown and KeyUp event procedures to handle any keystroke not recognized by the KeyPress event, such as function keys, navigation keys, and any combinations of these with keyboard modifiers (ALT, SHIFT, or CTRL keys). Unlike the KeyDown and KeyUp events, the KeyPress event doesn't indicate the physical state of the keyboard; instead, it indicates the ANSI character that corresponds to the pressed key or key combinations.

KeyPress interprets the uppercase and lowercase of each character as separate key codes and, therefore, as two separate characters.

Note The BACKSPACE key is part of the ANSI character set, but the DEL key isn't. If you delete a character in a control by using the BACKSPACE key, you cause a KeyPress event; if you use the DEL key, you don't

Why not just use the KeyDown event (switching KeyAscii to KeyCode) as that will work?
 

GregSmith

Registered User.
Local time
Yesterday, 19:40
Joined
Feb 22, 2002
Messages
126
Mile-O-Phile,

Do you have a code example on how to use the keydown event.
I swapped everything containing KeyAscii to KeyCode and the DELETE still
not fire for me.

=======================================================

Mile-O-Phile said:
The answer is in the help files under KeyPress event. :rolleyes:



Why not just use the KeyDown event (switching KeyAscii to KeyCode) as that will work?
 

ghudson

Registered User.
Local time
Yesterday, 20:40
Joined
Jun 8, 2002
Messages
6,195
You can use the "forms" Key Down event to detect if a trapped key is pressed and then override that keys normal function with another action or just exit the sub. The forms Key Preview property must be set to Yes and the KeyCode must be = 0 to prevent that keys normal function from happening, when pressed.

Below is an example of the forms Key Down event where I am using a Case Select to trap specific key presses. Check the help files for the Keycode Constants to get the codes for all keys.
Code:
'The forms Key Preview property must be set to Yes
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    Select Case KeyCode
        Case vbKeyDelete
            MsgBox "The Delete key was Pressed"
            KeyCode = 0
        Case vbKeyF1
            MsgBox "The F1 key was Pressed"
            KeyCode = 0
        Case vbKeyF2
            MsgBox "The F2 key was Pressed"
            KeyCode = 0
        Case Else
            'MsgBox "No match!" 'testing
    End Select
        
End Sub
Just REM out the message boxes when you are ready to go live. Then nothing will happen when the trapped keys are pressed. Turning this into a public function would be nice if that were possible.

HTH
 

mohammadagul

PrinceAtif
Local time
Today, 04:40
Joined
Mar 14, 2004
Messages
298
code fo keydown event

SELECT Case keycode
case vbkeyDelete
"Enter Your Command Here"
Case Else
End Select
 

GregSmith

Registered User.
Local time
Yesterday, 19:40
Joined
Feb 22, 2002
Messages
126
THAT DID IT!!

Thanks a bunch for the replies!! :D

ghudson said:
You can use the "forms" Key Down event to detect if a trapped key is pressed and then override that keys normal function with another action or just exit the sub. The forms Key Preview property must be set to Yes and the KeyCode must be = 0 to prevent that keys normal function from happening, when pressed.

Below is an example of the forms Key Down event where I am using a Case Select to trap specific key presses. Check the help files for the Keycode Constants to get the codes for all keys.
Code:
'The forms Key Preview property must be set to Yes
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    Select Case KeyCode
        Case vbKeyDelete
            MsgBox "The Delete key was Pressed"
            KeyCode = 0
        Case vbKeyF1
            MsgBox "The F1 key was Pressed"
            KeyCode = 0
        Case vbKeyF2
            MsgBox "The F2 key was Pressed"
            KeyCode = 0
        Case Else
            'MsgBox "No match!" 'testing
    End Select
        
End Sub
Just REM out the message boxes when you are ready to go live. Then nothing will happen when the trapped keys are pressed. Turning this into a public function would be nice if that were possible.

HTH
 

Users who are viewing this thread

Top Bottom