Same Keypress captured across multiple charts ids

Hi,

I am capturing keystrokes in my indicator afl. But the keystrokes are captured across different chart ids. In my case, I have different charts as well as panes in different sheets. I am not adding more than one pane in a sheet. I am also maintaining a gap of 3 secs between capture of keystrokes by storing the time in a static var. Also note that I have RequestTimedRefresh(1) in my afl.

F6, F7 and F11 are the keystrokes that I am capturing.

Chart with id 1881 is in "Sheet 2" under "Chart tab 2"
Chart with id 1882 is in "Sheet 3" under "Chart tab 1"
Chart with id 1883 is in "Sheet 4" under "Chart tab 1"
Chart with id 1884 is in "Sheet 1" under "Chart tab 2"

Please see below trace that confirms the capture of keystrokes across multiple chart ids.
ChartId = 1884 F6 Formulas\Drag-drop\AAAAAA 10.afl 523 42 12:56:25.19
ChartId = 1882 F6 Formulas\Drag-drop\AAAAAA 8.afl 523 42 12:56:25.19
ChartId = 1880 F6 Formulas\Drag-drop\AAAAAA 6.afl 523 42 13:06:49.49
ChartId = 1882 F6 Formulas\Drag-drop\AAAAAA 8.afl 523 42 13:06:49.49
ChartId = 1883 F6 Formulas\Drag-drop\AAAAAA 9.afl 523 42 13:45:18.31
ChartId = 1884 F6 Formulas\Drag-drop\AAAAAA 10.afl 524 42 13:45:18.31
ChartId = 1883 F7 Formulas\Drag-drop\AAAAAA 9.afl 530 42 13:45:24.33
ChartId = 1884 F7 Formulas\Drag-drop\AAAAAA 10.afl 531 42 13:45:24.33
ChartId = 1881 F11 Formulas\Drag-drop\AAAAAA 7.afl 551 42 13:45:40.31
ChartId = 1883 F11 Formulas\Drag-drop\AAAAAA 9.afl 551 42 13:45:40.31

Following are the checks I perform before a keystroke is considered as pressed.

   vk_F6 = 117;
   staticVarSuffix = "_" + NumToStr(GetChartID(), 1.0, False);
   KeyStrokeDateTime = Nz(StaticVarGetText("KeyDateTime" + staticVarSuffix), 0);
   timeDiff = DateTimeDiff(Now(5), KeyStrokeDateTime);
   if (timeDiff > 2 AND Status("action") == actionIndicator)
   {
      if (GetAsyncKeyState(vk_F6) < 0)
      {
         _TRACE("ChartId = " + ChartId + " F6");
         KeyPress = vk_F6;
         KeyStrokeDateTime = Now(5);
      }
   }
   StaticVarSetText("KeyDateTime" + staticVarSuffix, NumToStr(KeyStrokeDateTime, 1.0, False));

I have searched as well as tried different ways, but not able to find a solution. Is there a way to check the current visible chart and make sure that only the visible chart captures the keystroke?

GetAsyncKeystate is completely equivalent to Microsoft function having same name http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293
and it does not bother for which window is selected and focus. It acts global.

The only thing you can do to make it work chart pane specific is adding additional check such as left mouse button click together with your keystroke combo.

Another option is check for chart ID together with keystroke. I.e.

If( your_keystroke_combo && GetChartID() == 1882) {
// do something
}

But in latter case you need to be aware that same chart ID may occurr multiple times if you use default chart or chart template. So always use new blank chart.

2 Likes

I am taking care to have different chart id by using a new blank chart.

Is there a way to check which chart id is currently visible or in focus? If I am able to get that info, I can easily add it as a additional check with my check for keystroke.