Avatar of TimHudspith
TimHudspith

asked on 

Form_KeyDown event in Access not firing when press Escape key

For some reason it fires when I press CTRL, but not ESC.

Any ideas?
Microsoft ApplicationsMicrosoft OfficeMicrosoft Access

Avatar of undefined
Last Comment
Jim Dettman (EE MVE)
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

According to this link, the KeyDown event does not occur when you press the ESC key if the form has a command button for which the Cancel property is set to Yes.

Try using the KeyUp event instead.
Avatar of TimHudspith
TimHudspith

ASKER

My question needs amending: this behaviour relates to the ListView_KeyDown event.
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

I don't understand. Is it not the Escape key anymore we're talking about?
TimHudspith,

What is your ultimate goal here...?

If you want to see if the ESC key was pressed for a control, then you may have to use the KeyPRESS event not the KeyDown event

Private Sub YouListBox_KeyPress(KeyAscii As Integer)
    If KeyAscii = 27 Then
        MsgBox "You pressed the ESC key"
    End If
End Sub


JeffCoachman
"ListView_KeyDown event"

Are you talking about the Microsoft ActiveX List View control - if so, there is no Keydown event.

User generated image
User generated image
TimHudspith,

MX is correct, ..
I saw "ListView", and thought "ListBox".
:-(

What does a ListView do for you that a Listbox cannot?

Still not clear on your ultimate goal here.
Avatar of TimHudspith
TimHudspith

ASKER

Sorry for the late response to this, but the list view (MS common control Lib.) does have a Keydown event, but it does not show in the event list in the properties window. It is only accessible through the form code module.

For some reason - unlike the Form_KeyDown handler - it will not capture a press on the Escape key.
Avatar of TimHudspith
TimHudspith

ASKER

I've requested that this question be deleted for the following reason:

no solution
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

I assume your trying to deselect an item you just selected in the listview control.  How about using the Form's KeyDown event along with ActiveControl like this:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 27 And Me.ActiveControl.Name = "ListView1" Then
        Me.ListView1.SelectedItem.Selected = False
    End If
End Sub

Open in new window

You would need to set the form's KeyPreview property to true for this to work.
"You would need to set the form's KeyPreview property to true for this to work."
Which may be the problem to start with ?

mx
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
I should also add with that the fact that there is no solution is not  the fault of those answering the question in all cases.

  Sometimes the answer is "you really can't to that", which in of itself, is an answer.

Jim.
OK ... I just added a ListView Ax Control to a Form.
I went into the code behind the LV ... and added a Msgbox in the KeyDown event.

It works for me - see images.

Be *sure* the Form's Key Preview is set to Yes.

User generated image

mx
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of TimHudspith
TimHudspith

ASKER

Thanks for responses. Jim Dettman - as you say, it is one of those things that just cannot be done.
Well ... apparently not with Key Down, but it can with Key Up ...

?
As MX has pointed out, it seems you can get what you want with Keyup.  Please check out his comments and if that's the case, I'll re-open the question so you can reassign points.

Jim.
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

Be *sure* the Form's Key Preview is set to Yes.
No need to do this.
You only need to set the form's KeyPreview property to Yes if you are using the Form's KeyUp, KeyDown, or KeyPress event just like in the example I gave earlier.

The KeyUp event of the ListView control works (as pointed out by MX).  No need to change the KeyPreview property.
"You only need to set the form's KeyPreview property to Yes if you are using the Form's KeyUp, KeyDown, or KeyPress event just like in the example I gave earlier."

The things one forgets. Thx for the reminder ... :-)

Jim ... no worries about reopening.  I was really just curious as to why this was not working at all.  And still, bit of odd behavior.



mx
<<Jim ... no worries about reopening. >>

 For future readers, it's always best to make sure things are as they should be.  If the technique solves the problem, it should be labled as such.

Jim.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo