Announcement

Collapse
No announcement yet.

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 (e.Key == Key.LeftCtrl && !cntKey && Click) {
    
    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 maiyudse, Today, 12:57 AM
      0 responses
      1 view
      0 likes
      Last Post maiyudse  
      Started by SmartArtsStudio, Yesterday, 11:24 PM
      0 responses
      3 views
      0 likes
      Last Post SmartArtsStudio  
      Started by kenz987, Yesterday, 10:06 PM
      0 responses
      3 views
      0 likes
      Last Post kenz987
      by kenz987
       
      Started by AaronKoRn, Yesterday, 07:24 PM
      0 responses
      8 views
      0 likes
      Last Post AaronKoRn  
      Started by Aquatrade, 05-15-2021, 11:46 AM
      1 response
      17 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X