HMI Keyboard Shortcuts: Configuring View Key Events

Karen Mitchell1 min read
HMI ProgrammingOther ManufacturerTutorial / How-to
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Implement user-defined keyboard shortcuts by binding a WithEvents view variable to ActiveView after a station opens. The key event works only after the station is open and the graphics view has keyboard focus.

Event Binding Requirements

Declare the view variable with WithEvents so its KeyDown event is available. In Application_StationAfterOpen, assign ActiveView to that variable. The evidence does not identify a product version or confirm whether changing views automatically updates this reference.

Configure the Keyboard Macro

Dim WithEvents myView As View

Private Sub Application_StationAfterOpen(ByVal Station As Station)
    Set myView = ActiveView
End Sub

Private Sub myView_KeyDown(ByVal Char As Long, _
                           ByVal RepCnt As Long, _
                           ByVal Flags As Long)
    Select Case Char
        Case 65
            MsgBox "You just pressed 'a'"
        Case 82
            MsgBox "You just pressed 'r'"
    End Select
End Sub

The handler evaluates Char. A value of 65 triggers the A message, while 82 triggers the R message. RepCnt and Flags are received but not used by this macro.

Test and Diagnose the Shortcut

  1. Open a station so Application_StationAfterOpen executes.
  2. Click inside the graphics view to give it keyboard focus.
  3. Press A and verify that the A message box appears.
  4. Press R and verify that the R message box appears.

If neither key responds, first confirm that myView was assigned to the intended ActiveView and that the graphics view has focus. If the operator changed views after the station opened, check whether myView still references the original view; the supplied code does not rebind it after a view change.

FAQ

Why does the HMI keyboard shortcut work only after I click the graphics view?

The KeyDown handler belongs to the bound view, so that graphics view must have keyboard focus before it receives the key event.

What key values trigger A and R in the view macro?

The macro handles Char value 65 for A and 82 for R.

Why did the keyboard macro stop working after changing views?

myView is assigned from ActiveView only when the station opens. Verify its current reference and rebind it when the intended active view changes.

Back to blog