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

how to call 2 distinct PreviewKeyDown methods from a 3rd one

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

    how to call 2 distinct PreviewKeyDown methods from a 3rd one

    Hello Chelsea,
    My question is related to your post
    https://ninjatrader.com/support/foru...973#post685973

    But the thread was closed.

    I have a problem with 2 PreviewKeyDown methods.

    On_quantitySelector_PreviewKeyDown()

    ChartControl_PreviewKeyDown()

    the 1st method executes an action to move the focus out of the QuantitySelector TextBox (after new size input, AND it requires the cursor to be in focus inside the QS Textbox to work and move the focus on the Chart Window).

    The 2nd method executes a regular BuyMar****der (BUT it needs as for the 1st method for the cursor to be in focus BUT THIS TIME on the Chart Window to work and enter the BuyMar****rder).

    My goal would be to combine the 2 methods by calling them 1 after the other with a 200ms pause inbetween.

    But the problem is with the focus.

    I thought of (untested yet) calling the 2 methods inside a 3rd method, but the issue would be to use what PreviewkeyDown method?
    It seems if I select On_quantitySelector_PreviewKeyDown() for the 3rd method as

    PHP Code:
    On_quantitySelector_PreviewKeyDown() // method 3
    {
       
    On_quantitySelector_PreviewKeyDown() // method 1

       
    wait 200ms

       ChartControl_PreviewKeyDown
    () // method 2


    it would only execute the 1st method because the 2nd requires the focus on the Chart AND also a ChartControl_PreviewKeyDown method as calling method (the 3rd method seems requiring to be both of the 1st and 3rd type for it to work for the focus and for the calling).

    The flow would be:

    0. Focus in the Qs TextBox ->
    1. user Presses the E Key ->
    1. the 3rd method (a second On_quantitySelector_PreviewKeyDown() one) calls the 1st method and does executes the focus move out of the QS TextBox
    2. With focus now on the Chart, method 3 calls method 2 which should work since the focus is the correct one, but doesn't work since the calling method (method 3) is of type On_quantitySelector_PreviewKeyDown() which only works if the focus is inside the QS TextBox.

    Vice-versa problem if I select the 2nd method as 3rd method type.

    What alternative workaround would you think of? Thanks!
    Last edited by PaulMohn; 06-17-2022, 04:54 PM.

    #2
    PaulMohn You could also consider using a common event handler that takes appropriate actions based on the UIElement that generates the event, perhaps along these lines:
    Code:
    ...
    <control>.PreviewKeyDown += OnPreviewKeyDown;  // Whatever control is relevant, for each control of interest
    ...
    private void OnPreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        Key key = e.Key;
    
        if (sender is ChartControl)
        {
        // Handle ChartControl
        }
        else
        if (sender is QuantityUpDown)
        {
        // Handle QuantityUpDown
        }
        // ... etc
    }
    You would need to know the specific controls that the event handler needed to be added to, and you would need to be sure that the events are being detected and handled correctly. This may require some experimentation with the controls you have mentioned. The first step might be to see if you can simply detect (preview) key press events for the controls you're interested in. When that is working, then code for the actions you want.

    You may be able to integrate your dependencies more easily in this manner. Just a thought.

    Thanks.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    Comment


      #3
      jeronymite thank you very much for the very useful sample and concepts. I'd have never thought you could assign the OnPreviewKeyDown to multiple Controls at once, nor that you could name the method as is without the control reference. But it works. Thanks again!

      Attached a preliminary working sample version with keys
      • D triggering the focus out of the QS Textbox + PreviewKeyDown event of ChartControl type (for Buy order logic, printing preliminary test works)
      • F triggering the focus out of the QS Textbox + PreviewKeyDown event of ChartControl type (for Sell order logic, printing preliminary test works)
      • C triggering the focus out of the QS Textbox (printing preliminary test works)
      Lines of interest
      https://pastecode.io/s/1s0wzyiw
      • 113
      • 128, 130
      • 168
      • 229-256
      • 266-338

      I think I'll use the snippets directly vs calling the methods as it seems it's working and simpler.

      I'll update and test with the ProfitSniper script and share on the User Apps Share asa. Many thanks again for the valuable direction and concepts!
      Attached Files
      Last edited by PaulMohn; 06-18-2022, 12:41 PM.

      Comment


        #4
        jeronymite upon further tests with your very handy (sender is ChartControl) mention,
        I found this new way (sender as ChartControl) from this tip.

        I realized I just needed it as

        PHP Code:
              private async void OnPreviewKeyDown(object senderSystem.Windows.Input.KeyEventArgs p)
              {
                
        await ChartControl.Dispatcher.InvokeAsync((Action) (() =>
                {
                  if(
        p.Key == Key.D)
                  {
                      
        Keyboard.Focus(sender as ChartControl); 

        in

        PHP Code:
             #region OnPreviewKeyDown / FocusOut &amp; EnterBuyMarket Order OR EnterShortmar****rder

              
        private async void OnPreviewKeyDown(object senderSystem.Windows.Input.KeyEventArgs p)
              {
                
        await ChartControl.Dispatcher.InvokeAsync((Action) (() =>
                {
                  if(
        p.Key == Key.D)
                  {
                    
        #region FocusOut of QS TextBox

                      
        Keyboard.Focus(sender as ChartControl);

                    
        #endregion

                    #region Enter BuyMar****rder

                      
        TriggerCustomEvent(=>
                      {
                        Print(
        "Buy");

                      }, 
        null);

                    
        #endregion

                  
        }
                  else
                  if(
        p.Key == Key.F)
                  {
                    
        #region FocusOut of QS TextBox

                      
        Keyboard.Focus(sender as ChartControl);

                    
        #endregion

                    #region Enter SellMar****rder

                      
        TriggerCustomEvent(=>
                      {
                        Print(
        "Sell");

                      }, 
        null);

                    
        #endregion

                  
        }
                  
        p.Handled true;

                }));
              }

            
        #endregion 

        to get the focus to the ChartControl (it also works with ChartPanel).

        Now it really gets the focus on the ChartControl working.

        My previous post would have worked as well with simply
        PHP Code:
              private async void OnPreviewKeyDown(object senderSystem.Windows.Input.KeyEventArgs p)
              {
                
        await ChartControl.Dispatcher.InvokeAsync((Action) (() =>
                {
                  if(
        p.Key == Key.D)
                  {
                      
        Keyboard.ClearFocus(); 

        But I noticed my other hotkey to focus back the cursor in the quantitySelector TextBox after clearing it
        wouldn't work unless replaced inside the quantitySelector.

        New script and attached
        Focus Out Of QuantitySelector TextBox & ChartControl event


        I'll test tomorrow with order submission and be back asa. Many thanks again!
        Attached Files
        Last edited by PaulMohn; 06-19-2022, 09:18 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by raffu, Yesterday, 11:41 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by cmtjoancolmenero, Yesterday, 11:56 AM
        1 response
        13 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by reynoldsn, 04-21-2024, 07:53 PM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by cmtjoancolmenero, Yesterday, 03:40 PM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by ageeholdings, 04-28-2024, 07:43 AM
        2 responses
        24 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X