2

Is there a way to add keydown and keyleft event handler to image object in a user form? I can only see mouseup/down/move,error,dragover,dropandpaste.

I notice with an option button, the event keydown/keyleft is available but not for image. Thanks in advance

5
  • Given that you can't type in an image control, no. What are you trying to use it for? Commented Jul 20, 2018 at 7:47
  • What is this KeyLeft event of which you speak? Commented Jul 20, 2018 at 7:49
  • Ah ok. Thanks @Rory is there anyway to code them in do you know? Commented Jul 20, 2018 at 7:49
  • You can't add events that don't exist without subclassing, but the image control can't receive focus. See my answer for a proxy approach. Commented Jul 20, 2018 at 7:50
  • @ThunderFrame I’m pretty sure it was in the drop down when selecting which event you want. I could be wrong. Commented Jul 20, 2018 at 8:16

2 Answers 2

3

You can't add event handlers that don't exist for that control type (other than by subclassing, but the image control doesn't receive focus anyway), but an image control can't receive the focus, and therefore can't receive keystrokes anyway.

You talk about KeyDown and KeyLeft events. While some controls have KeyDown, KeyPress and KeyUp events, there aren't any controls that have a KeyLeft event. Do you perhaps mean to capture the KeyDown event, and then detect whether the Down arrow or Left arrow was the key that pressed down?

However, there are a few options:

  1. Wire up the KeyDown or KeyPress event (depending upon which type of key information you require) for the UserForm, and then act upon the image control. This is probably only of use if you're happy for keypresses to be handled by the form, regardless of which other control might be selected.

  2. Add a proxy control, say a TextBox that does receive KeyDown and KeyPress events, and place it behind the image control. That way, when the user tabs to the textbox, you can add a border to the image to make it look like it is selected, and then have the textbox key events act upon the image. If a user clicks on the image, instead of tabbing between controls, you can set the image's click event to give the textbox the focus, and again, handle the key events for the textbox as a proxy for the image control.

Sign up to request clarification or add additional context in comments.

2 Comments

this seems to be a great workaround. I will try it later and let you know how I get on. Thanks
I used the frame and inserted a picture to the frame. Now when the frame moves, so does the picture. I wasn't able to use the arrow keys so I used WSAD instead and works a treat. Thanks
2

It depends on what are you trying to achieve which you didn't specify.

Image obviously doesn't have Key-events so it can't be used directly. One possible solution could be to use Frame control which has a Picture property and has KeyDown event, so you can catch key-downs when the frame has focus. Example:

Private Sub FrameWithPicture_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    ' Compare KeyCode with predefined vbKey-Constansts, e.g. for key-left:
    If KeyCode = vbKeyLeft Then
        ' left arrow key was pressed, do somthing
    Else
        ' all other keys were pressed, do something else 
    End If
End Sub

Or you could add Image control to this frame and when key-down event occurs on the frame, then perform some action on this image like this: FrameWithImage.Controls.Item(0).BackColor = vbRed etc. HTH

7 Comments

How does the frame receive focus, if there aren't any selectable controls within it?
@ThunderFrame frame can receive focus even without controls. Tabstop must be set to true.
@ThunderFrame or maybe not :). If you are right, then it is necessary to add image control into the frame.
@ThunderFrame OK, I have checked that when an image is inside of frame then the frame gets focus on tab and it receives the key-down events.
Good to know. Nice solution. I guess you can remove the frame border too?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.