Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

OnKeyDown()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    OnKeyDown()

    I think this is outside the scope of NT support, but if anyone else knows how this would work, it would be great if you could give me a hint. I'm trying to have a strategy detect a keystroke, and after spending some time reading I came up with the following:

    PHP Code:
    protected void OnKeyDown(KeyEventArgs keyEvent)
                {
                if (
    keyEvent.KeyCode == Keys.Insert)
                    Print(
    "ins!");
                } 
    This compiles fine, but when I press the insert key while the strategy is running, nothing happens. Another thing I noticed is that NOTHING seems to work inside this method, like I added a line to print some text if Close[0] > 0, and nothing prints. So does anyone have any ideas as to why this isn't working? I have included the System.Windows.Forms namespace, which I guess is required. Thanks.

    #2
    Originally posted by Radical View Post
    I think this is outside the scope of NT support, but if anyone else knows how this would work, it would be great if you could give me a hint. I'm trying to have a strategy detect a keystroke, and after spending some time reading I came up with the following:

    PHP Code:
    protected void OnKeyDown(KeyEventArgs keyEvent)
                {
                if (
    keyEvent.KeyCode == Keys.Insert)
                    Print(
    "ins!");
                } 
    This compiles fine, but when I press the insert key while the strategy is running, nothing happens. Another thing I noticed is that NOTHING seems to work inside this method, like I added a line to print some text if Close[0] > 0, and nothing prints. So does anyone have any ideas as to why this isn't working? I have included the System.Windows.Forms namespace, which I guess is required. Thanks.
    you have to assign the event. like in OnStartUp

    ChartControl.ChartPanel.KeyDown += new KeyEventHandler(OnKeyDown);

    do remove the event at OnTermination.

    coding from memory so there can be syntax err

    Comment


      #3
      Thanks for the tip. When I add that to my code:

      PHP Code:
              protected override void OnStartUp()
              {
              
      ChartControl.ChartPanel.KeyDown += new KeyEventHandler(OnKeyDown);
              }
              
              protected 
      void OnKeyDown(KeyEventArgs keyEvent)
                  {
                  if (
      keyEvent.KeyCode == Keys.Insert)
                      Print(
      "ins!");
                  }   
             protected 
      override void OnTermination()
              {
                  
      ChartControl.ChartPanel.KeyDown -= new KeyEventHandler(OnKeyDown);
              } 
      I get the error "No overload for 'OnKeyDown' matches delegate 'System.Windows.Forms.KeyEventHandler' for the line where I assign it and the line where I remove it.
      Last edited by Radical; 12-04-2011, 10:16 PM.

      Comment


        #4
        the method should be like

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
        //do your stuff
        }

        Comment


          #5
          Wow, that worked, thanks! I had seen the "object sender, KeyEventArgs e" on the MSDN website and I tried putting them in various places, but I didn't try it in OnKeyDown. Thanks again.

          Comment


            #6
            Note using "new KeyEventHandler" as above is superfluous. You can just use the method name directly as the compiler will figure it out.

            protected override void OnStartUp()
            {
            ChartControl.ChartPanel.KeyDown += MyEventHandlerMethod;
            }

            protected override void OnTermination()
            {
            ChartControl.ChartPanel.KeyDown -= MyEventHandlerMethod;
            }

            public void MyEventHandlerMethod(object sender, KeyEventArgs e)
            {
            Print("It works!");
            }

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Tim-c, Today, 10:58 AM
            0 responses
            1 view
            0 likes
            Last Post Tim-c
            by Tim-c
             
            Started by traderqz, Yesterday, 09:06 AM
            3 responses
            21 views
            0 likes
            Last Post NinjaTrader_ThomasC  
            Started by f.saeidi, Today, 10:19 AM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by kujista, Today, 06:23 AM
            5 responses
            18 views
            0 likes
            Last Post kujista
            by kujista
             
            Started by traderqz, Today, 12:06 AM
            3 responses
            6 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Working...
            X