Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy control its Ninja Chart keyboard focus?

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

    Strategy control its Ninja Chart keyboard focus?

    Hi, I'm developing a "standalone" unmanaged strategy which has its own Winforms interface.
    Yes, I know about WPF but this older approach is, for me at least, an easier approach, and
    it's working fine. But since the strategy is started by, and associated with a NinjaTrader "host"
    Chart, and I'm wanting to use the Chart as part of the interaction...

    I'm wondering how I can force keyboard focus to the Chart; and then, of course, I can take it
    back again. So it doesn't sound too difficult; so what would be the most reliable way for my
    Strategy to ask its Chart to request focus.

    I mean, I guess I could hack in some Auto Hotkey Script which my Winforms app could invoke
    to cause focus to be shifted to the Ninja Form, in a specific location on the screen. That's a
    hack that I could easily use, but maybe an easier solution?

    Obviously, to get Cntl key up/down states, and Mouse Clicks, the Chart must have focus. I toyed
    with the idea of doing my own simple chart, which would eliminate this issue; but I'd like to use
    the NinjaTrader Chart's great features, as part of the overall interactive solution.

    [edit] I do have this little DLL I once wrote, which can programmatically
    simulate click anywhere on the desktop; so I could give focus on the chart
    in that way as well...

    Thank you !
    Last edited by Hyper; 09-07-2020, 08:44 PM. Reason: more info

    #2
    Hello Hyper,

    Thanks for your post.

    While working with another user we faced some struggles trying to force focus after clicking an added button in ChartTrader when using ChartControl.PreviewKeyUp and ChartControl.PreviewDown. I understood this issue to involve trying to force focus on ChartControl as opposed to forcing focus on a ChartPanel. We document the IsFocused property for ChartPanel, but we do not do so for ChartControl.

    The snippet below can be used to observe focus on a ChartPanel and the two lines of code that are commented can be uncommented to force focus on a ChartPanel.

    Code:
    protected override void OnBarUpdate()
    {
        if (State == State.Historical)
            return;
    
       foreach (var window in NinjaTrader.Core.Globals.AllWindows)
       {
          // check if the found window is a Chart window, if not continue looking
          if (!(window is NinjaTrader.Gui.Chart.Chart)) continue;
    
          window.Dispatcher.InvokeAsync(new Action(() =>
          {
             // try to cast as a Chart, if it fails it will be null
             var foundChart = window as NinjaTrader.Gui.Chart.Chart;
    
             // make sure we found a chart
             if (foundChart == null) return;
    
             // make sure the found chart is the owner window
             if (foundChart == this.Owner as NinjaTrader.Gui.Chart.Chart)
             {
                if (foundChart.ActiveChartControl.ChartPanels[ChartPanel.PanelIndex] == ChartPanel)
                {
                   ClearOutputWindow();
    
                   //foundChart.ActiveChartControl.ChartPanels[0].Focus();
                   //Keyboard.Focus(foundChart.ActiveChartControl.Chart Panels[0]);
    
                   Print(ChartPanel.IsFocused.ToString());
                   Print(foundChart.ActiveChartControl.IsFocused);
                   Print(foundChart.ChartTrader.IsFocused);
                   Draw.TextFixed(this, "Status", ChartPanel.IsFocused.ToString(), TextPosition.BottomLeft);
                }
             }
          }));
       }
    }
    I have had luck using a WinForms timer and to check hot key presses from there. I have attached an example that was used to demonstrate.

    It may also be of use if you shared where your DLL can be found if others want to consider that approach for an unsupported way to force focus.

    We look forward to assisting.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      I am very appreciative of your help; and of all you guys in Platform Technical Support !!
      hyperscalper

      Comment


        #4
        Alternative Solution

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by f.saeidi, Yesterday, 12:14 PM
        9 responses
        23 views
        0 likes
        Last Post f.saeidi  
        Started by Tim-c, Today, 03:54 AM
        0 responses
        3 views
        0 likes
        Last Post Tim-c
        by Tim-c
         
        Started by FrancisMorro, Today, 03:24 AM
        0 responses
        3 views
        0 likes
        Last Post FrancisMorro  
        Started by Segwin, 05-07-2018, 02:15 PM
        10 responses
        1,772 views
        0 likes
        Last Post Leafcutter  
        Started by Rapine Heihei, 04-23-2024, 07:51 PM
        2 responses
        31 views
        0 likes
        Last Post Max238
        by Max238
         
        Working...
        X