View Single Post
  #9 (permalink)  
Old 02-14-2009, 09:03 AM
pips4life pips4life is offline
Junior Member
 
Join Date: Apr 2007
Posts: 7
pips4life is on a distinguished road
A challenge to capture mouse-position in a hotkeyed script

Hi MT4 programming experts,

Over on the MQL4 web site, several people have asked if they could capture keyboard or mouse clicks from within a program. The typical answer is no, and yet I've seen some very clever applications that use a DLL to capture or emulate keyboard events. What I want to do is capture a mouse event, for which I have found no useful MQL4 examples.

What am I trying to accomplish? I want to define hotkeys to save me some keystrokes. It's especially of interest to me because I have a Logitech G11 programmable keyboard with 18 extra buttons on the left. It sure would be nice to be able to program those readily accessible keys to create customized objects.

For example, I want to mark up my charts with Elliott Wave counts and while I can press the Text button (same as Insert->Text) to create a text, I always have to edit the Description so it says "1", and the next one "2", etc.

I wrote a simple script which can create a text object. It works, but only if you drag-and-drop the script from the Navigator window onto the chart. It makes use of the "WindowPriceOnDropped" and "WindowTimeOnDropped" functions which are specifically for this purpose.

Next, I defined a hotkey to run my script. (I used Cntl-W since it wasn't already used).

When I press "Cntl-W", sure it runs my script, but the Time/Price values are both zero because I did not use the drag-and-drop method.

I want to capture the mouse position and have those coordinates be used by the MT4 functions, so...

I saw mention of the "mouse_event" function within the WinUser32.mqh library that comes with MT4. I think I figured out how to press and release the left button but it does not help, because the WindowPriceOnDropped function ignores it. Obviously it wasn't a drag-and-drop of this script onto a chart but I was hoping to get lucky.

Here's my attempt at the code (which works to create a text object with drag-and-drop, but does not work when using a hotkey):

Code:
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <WinUser32.mqh>
//void     mouse_event(int dwFlags,int& dx,int& dy,int dwData,int dwExtraInfo);
//#define MOUSEEVENTF_LEFTDOWN           0x0002 // left button down
//#define MOUSEEVENTF_LEFTUP             0x0004 // left button up


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
  GetFocus();
  mouse_event(2,0,0,0,0);
  Sleep(10); // milliseconds
  mouse_event(4,0,0,0,0);
  double price = WindowPriceOnDropped();
  datetime time = WindowTimeOnDropped();
  Print("Price at drop: ",NormalizeDouble(price,2));
  
  if (price == 0 || time == 0) return(1); // If price and/or time is 0, it failed so exit early
  
  ObjectDelete("Text1");
  ObjectCreate("Text1",OBJ_TEXT,0,time,price);
  ObjectSetText("Text1","Hello",18,"Arial",Red);
//----
   return(0);
  }
//+------------------------------------------------------------------+
A picture is attached. The Experts log will print the correct price if it works.


Thanks in advance for any help trying to solve this problem!
Pips4life

P.S. Some links I found when searching for clues might be helpful:
WinUser32.mqh - MQL4 Code Base (WinUser32.mqh info)
mouse_event Function () (mouse_event doc)
Breakpoints in Tester: It's Possible! - MQL4 Articles (Breakpoints in Tester)
ind_HotKeys_v1.mq4 - MQL4 Code Base (A keybd_event simple example)
Attached Images
File Type: jpg test hotkey.jpg (90.8 KB, 576 views)
Reply With Quote