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

On KeyPressDown and Click

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

    On KeyPressDown and Click

    Hi,

    I am looking into combining two function to print a condition.

    I took the example from two different posts on the forum showing the function for keydown and the function click to get the price data but I am not sure how to create a condition that will map both the function.

    As a reference code to get the keydown press and code to get the coordinate of the click

    Code:
    
    if (State == State.DataLoaded)
    {
    
    if (ChartPanel != null)
    {
    
    this.ChartPanel.KeyDown += new System.Windows.Input.KeyEventHandler(OnKeyDown);
    this.ChartPanel.KeyUp += new System.Windows.Input.KeyEventHandler(OnKeyUp);
    
    }
    }
    
    
    else if (State == State.Historical)
    {
    if (ChartControl != null)
    {
    foreach (ChartScale scale in ChartPanel.Scales)
    if (scale.ScaleJustification == ScaleJustification)
    chartScale = scale;
    
    ChartControl.MouseLeftButtonDown += MouseClicked;
    }
    }
    
    
    else if (State == State.Terminated)
    {
    
    if (ChartPanel != null)
    {
    
    this.ChartPanel.KeyDown -= OnKeyDown;
    this.ChartPanel.KeyUp -= OnKeyUp;
    ChartControl.MouseLeftButtonDown -= MouseClicked;
    
    }
    }
    
    
    
    }
    
    
    private bool cntKey = false;
    public void OnKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
    if (e.Key == Key.LeftCtrl && !cntKey)
    {
    cntKey = true;
    Print("Down");
    }
    }
    
    public void OnKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
    if (e.Key == Key.LeftCtrl && cntKey)
    {
    cntKey = false;
    // Print("Up");
    }
    }
    
    
    protected override void OnBarUpdate()
    {}
    //Add your custom indicator logic here.
    
    
    
    protected void MouseClicked(object sender, MouseButtonEventArgs e)
    {
    
    
    // convert e.GetPosition for different dpi settings
    clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.Get Position(ChartControl as IInputElement).X, ChartControl.PresentationSource);
    clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartControl as IInputElement).Y, ChartControl.PresentationSource);
    
    convertedPrice = Instrument.MasterInstrument.RoundToTickSize(chartS cale.GetValueByY((float)clickPoint.Y));
    
    convertedTime = ChartControl.GetTimeBySlotIndex((int)ChartControl. GetSlotIndexByX((int)clickPoint.X));
    
    Draw.TextFixed(this, "priceTime", string.Format("Price: {0}, Time: {1}", convertedPrice, convertedTime), TextPosition.BottomLeft);
    
    ForceRefresh();
    }

    Now what I want to achieve is something where I can combine the two function in one condition to output


    Code:
    if ([B][COLOR=#3498db]e.Key == Key.LeftCtrl && !cntKey[/COLOR][/B] [COLOR=#9b59b6]&& [B]Click[/B][/COLOR]) {
    
    Draw.TextFixed(this, "priceTime", string.Format("Price: {0}, Time: {1}", convertedPrice, convertedTime), TextPosition.BottomLeft);
    
    }

    Thanks

    #2
    Hello papaoutai, thanks for writing in.

    In the Click even you will need to check for IsKeyDown (publicly available link):
    https://docs.microsoft.com/en-us/dot...n?view=net-5.0

    This way you can get the event when the mouse is clicked, then within that event you can check if a specific key is pressed and execute code if that method returns true.

    Best regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Amazing works like a charm. Thanks for helping

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by TheMarlin801, 10-13-2020, 01:40 AM
      20 responses
      3,914 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by timmbbo, 07-05-2023, 10:21 PM
      3 responses
      151 views
      0 likes
      Last Post grayfrog  
      Started by Lumbeezl, 01-11-2022, 06:50 PM
      30 responses
      808 views
      1 like
      Last Post grayfrog  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      3 responses
      11 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by Johnny Santiago, 10-11-2019, 09:21 AM
      95 responses
      6,194 views
      0 likes
      Last Post xiinteractive  
      Working...
      X