About Keyboard event handle in RhinoCommon

Hi:
When I run this script, if I want to rotate the viewport, I must put the cursor on the “Press J or K” window, or it just iuput string to rhino.
I need wherever my cursor is in rhino, I will rotate the viewport by key “J” or “K”, think you.

  import Rhino
  import System.Drawing
  import System.Windows.Forms
  import scriptcontext
  class Form1(System.Windows.Forms.Form):

      def __init__(self):

          self.InitializeComponent()

      def InitializeComponent(self):
          self._button = System.Windows.Forms.Button()
          self.SuspendLayout()
          self._button.Location = System.Drawing.Point(150, 67)
          self._button.Size = System.Drawing.Size(75, 23)
          self._button.TabIndex = 0
          self._button.Text = "Done"
          self._button.Click += self.ButtonClick
          self.ClientSize = System.Drawing.Size(250, 102)
          self.Controls.Add(self._button)
          self.KeyPreview = True
          self.Text = "Press J or K"
          self.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
          self.KeyDown += self.OnKeyDownEvent
          self.Closing += self.OnClosingEvent
          self.ResumeLayout(False)

      def ButtonClick(self, sender, e):
          self.Close()

     def OnKeyDownEvent(self, sender, e):
         left = e.KeyCode == System.Windows.Forms.Keys.J
         right = e.KeyCode == System.Windows.Forms.Keys.K
         if left or right:
             amount = 0.1
             if right: amount = -0.1
                viewport = scriptcontext.doc.Views.ActiveView.MainViewport
                z = viewport.ConstructionPlane().ZAxis
               viewport.Rotate(amount, z, Rhino.Geometry.Point3d.Origin)
             scriptcontext.doc.Views.Redraw()
          finish = e.KeyCode == System.Windows.Forms.Keys.Escape
          finish = finish or e.KeyCode == System.Windows.Forms.Keys.Enter
          if finish:
              self.Close()
          e.Handled = True
      def OnClosingEvent(self, sender, e):
          Rhino.Input.Custom.GetBaseClass.PostCustomMessage("exit")

  f = Form1()
  f.Show(Rhino.RhinoApp.MainWindow())
  gs = Rhino.Input.Custom.GetPoint()
  gs.AcceptCustomMessage(True)
  gs.Get()
  if not f.IsDisposed:
      f.Close()

Hi @pythonuser,

i guess if the OnKeyDownEvent is part of the form class you can only get the event to fire if the form has focus. But you´re putting the focus to the Rhino main window by using the GetPoint() method asking for a point. This requires that Rhino has the focus and not the form window, so you can enter a point to the command line instead of picking it. To force the focus on the form window, it can be made non modal, if you change it like this:

f.ShowDialog(Rhino.RhinoApp.MainWindow())

however, doing this you loose the ability to pick the point. I´m wondering if you could add custom command line options to the GetPoint() method for Rotate_Left and Rotate_Right while the Rhino.Input is called. This way you may fire the rotation if the option has been clicked in the commandline or if the option key (+Enter) has been used while the dialog remains open.

c.

Hi @clement,
Thinks for your reply, you are right, if i use the method f.ShowDialog(Rhino.RhinoApp.MainWindow()) , i would miss the GetPoint() method.
i need change it because i need orient object on surface, and i have make the preview by Rhino.Display.DisplayPipeline.DrawObject(), i just want to rotate the object in the process of orientation, but it seem i have not other choice to get the results which i need.
Thinks for your help again clement.