Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

VBA Word Form with VBA: how can I detect when the user has used an arrow key to select a different button?

Using VBA with Word, I have a form similar to a MsgBox on steroids, with three or four buttons at the bottom. One button is currently selected and, as such, has a different colour than the other buttons. If a user presses an arrow key to move the selection to another button, how can I detect that the selection has moved?
     I need then to change the colour of the newly-selected button to that different colour and make the colour of the original button match that of the other unselected buttons.
          —John Robin (Allen) in Priddis, Alberta
Avatar of DrTribos
DrTribos
Flag of Australia image

Here is the basic structure:

You need to find the key code for the arrow keys.
Private Sub cbAttribute_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyF1 Then 
        ' Your code here
    End If
End Sub

Open in new window


Hint: check this list https://msdn.microsoft.com/en-us/library/aa243025(v=vs.60).aspx
Avatar of JohnRobinAllen

ASKER

I am sure that that is the solution, even before I try it. Full credit given. My original query was to find out what is currently selected after I moved the selection with the arrows. I guess that will be clear when I put a Stop inside the code above.
     Thanks, once again, to the celebrated Dr. Tribos.
     j.r.a.
Ahhh I missed your intent. I think you mean what is selected on the userform? I'm sure you can get that from one of the events, you'd have to add code to each button to detect focus change. .?
I tried the code and nothing happened. I'm sure the problem is in line 1.

Private Sub cbAttribute_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

     The two buttons I'm concerned about are vbKeyRight and vbKeyLeft, so I changed line 2 to put both of those in the Sub. I also put a "Stop" in the code to see if I got into the sub. I didn't, and I suspect that I have to change, in line 1 above, "cbAttribute" and "MSForms" to something in my form.
     I appreciate the help I'm getting, but like Sisyphus, I just can't get that rock up to the top of the mountain.
          --j.r.a.
CbAttribute is the name of a  combobox on my form... yes... you'll have to rename that part...
Create a blank form, nothing on it.  In design view, double click then add this code:

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Debug.Print CStr(KeyCode)
' Debug.Print Me.ActiveControl   <-- uncomment after putting controls on form to see what is active
End Sub

Open in new window


Make sure immediate window is visible Ctrl+G

HTH
I did as you said. I made a new form, UserForm, and put a command button on it, CommandButton1.
Then I put your code into it, along with a couple of other subroutines:
Private Sub Commandbutton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
      Debug.Print CStr(KeyCode)
      Debug.Print Me.ActiveControl   '<-- uncomment after putting controls on form to see what is active
End Sub
Private Sub CommandButton1_Click()
      Stop
End Sub

Private Sub UserForm_Click()
      Stop
End Sub

Nothing happens when I click on the button or on the form.

I'm sorry to be so incompetent.

     j.r.a.
Change:
Private Sub CommandButton1_Click()
      Stop
End Sub

Open in new window


to this:
Private Sub CommandButton1_Click()
      msgbox "Boo"
End Sub

Open in new window


Then with cursor in the code press F5 (to start the form) and then press F5 to fire the macro.

If you don't see boo you have some other issue - I'm pretty sure from your other questions that macros are enabled... so some other issue?
That works. I also added another button to the form, coded it as you suggest, but with a different output message.
     However, my original question was to know which of two or three buttons is currently selected as the default, the button that gets “clicked” when a user presses the Enter key. The Right or Left arrow keys do that automatically to enhance the current default button and set it as the default button. The original solution you proposed suggested we could figure out the default by knowing which buttons were clicked.
     I want to enhance it more by changing the colour of the current default button. How would one do that?
     Your code triggers an action when a user clicks on a button. My goal is to let the user make a choice with the Arrow Keys and then press the Enter key in lieu of clicking on a button. VBA does that automatically, but I want to enhance the selected button by changing its colour.
     I can’t even find out which arrow key was pressed.
     Here is my current code, in which the two buttons on the form are named “Jack” and “Jill.” I tried to get it to respond to pressing the Right arrow key, but that did not work.
     In general, I would prefer not to have to figure out the current default by which arrow key was pressed to move to it, but if that is the only way to find out the current default, I’ll either go with that or give up trying to highlight the current default. Here is my current code, including an unsuccessful attempt to detect when and arrow key was pressed:

Option Explicit
Private Sub Jack_Click()
      MsgBox "Pressed button 'Jack'"
End Sub
Private Sub Jill_Click()
      MsgBox "Pressed button 'Jill'"
End Sub
Private Sub vbKeyRight_KeyDown()
      MsgBox "Boo"
End Sub
     
     One other word: I greatly appreciate all the time you have spent on this question. You are a saint.
     —jra.
Ahhhh...

More like this (user form with x2 command buttons):

Option Explicit

Private Sub CommandButton1_Enter()
    Me.CommandButton2.BackColor = Me.CommandButton1.BackColor
    Me.CommandButton1.BackColor = vbGreen
End Sub

Private Sub CommandButton2_Enter()
    Me.CommandButton1.BackColor = Me.CommandButton2.BackColor
    Me.CommandButton2.BackColor = vbGreen
End Sub

Open in new window


Then you can use the left right arrows to toggle.... and the color of the 'active' button changes
ASKER CERTIFIED SOLUTION
Avatar of DrTribos
DrTribos
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This looks promising or better. Unfortunately I cannot check it out until tomorrow morning.
     jra
I cannot believe how beautifully that code works. It blows my mind, it solves my problems, it makes my day.
A very big, grateful thanks to Dr. Tribos. May the Fourth be with you!

    In appreciation,
    J.r. in Priddis
A beautiful, simple solution.
You're most welcome. Cheers