Unpressing/undepressing buttons

 

Hello I have this code for my test button:


int OnInit()
  {
      if(!ButtonCreate(0,InpName,0,150,150,200,100,InpCorner,"Press",InpFont,InpFontSize,
      InpColor,InpBackColor,InpBorderColor,InpState,InpBack,InpSelection,InpHidden,InpZOrder))
     {
     return(0);
    }

   ChartRedraw();

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(sparam==bh) {
      Alert("dlcjwnd");
  }
}
//+------------------------------------------------------------------+

I hit the button and the Alert pops up all good.  But the button stayed looking pressed.  I also did this:

                Sleep(500);
                ObjectSetInteger(0,bh,OBJPROP_STATE,false);

That makes the button still work, but not self-unpress given that is what I've done with the button and my mouse click on it.

How can I get the button unpress-looking automatically, having clearly pressed-looking onClick?

 
ObjectSetInteger(0,bh,OBJPROP_STATE,false);

What does bh refer to? The button's name is InpName

 
GumRai:

What does bh refer to? The button's name is InpName

Hello it is just because of the way it was pasted in - I've corrected the OP code.  The question still stands.
 
Show the code that you are actually using.
 
GumRai:
Show the code that you are actually using.
//+------------------------------------------------------------------+
//|                                                      afxBtns.mq4 |
//|                  Copyright 2016, afx |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, afx"
#property link      ""
#property version   "1.00"
#property indicator_chart_window

extern int btnW = 70;
extern int btnH = 16;
extern int btnX = 220;
extern int btnY = 1;
extern int btnXSpacer = 10;
extern int btnYSpacer = 4;

#import "user32.dll"

void keybd_event(int bVk, int bScan,int dwFlags,int dwExtrainfo); 

#define KEYEVENTF_EXTENDEDKEY          0x0001
#define KEYEVENTF_KEYUP                0x0002

#define VK_0   48
#define VK_1   49
#define VK_2   50
#define VK_3   51
#define VK_4   52
#define VK_5   53
#define VK_6   54
#define VK_7   55
#define VK_8   56
#define VK_9   57
#define VK_A   65
#define VK_B   66
#define VK_C   67
#define VK_D   68
#define VK_E   69
#define VK_F   70
#define VK_G   71
#define VK_H   72
#define VK_I   73
#define VK_J   74
#define VK_K   75
#define VK_L   76
#define VK_M   77
#define VK_N   78
#define VK_O   79
#define VK_P   80
#define VK_Q   81
#define VK_R   82
#define VK_S   83
#define VK_T   84
#define VK_U   85
#define VK_V   86
#define VK_W   87
#define VK_X   88
#define VK_Y   89
#define VK_Z   90

#define VK_LBUTTON         1     //Left mouse button
#define VK_RBUTTON         2     //Right mouse button
#define VK_CANCEL          3     //Control-break processing
#define VK_MBUTTON         4     //Middle mouse button (three-button mouse)
#define VK_BACK            8     //BACKSPACE key
#define VK_TAB             9     //TAB key
#define VK_CLEAR           12    //CLEAR key
#define VK_RETURN          13    //ENTER key
#define VK_SHIFT           16    //SHIFT key
#define VK_CONTROL         17    //CTRL key
#define VK_MENU            18    //ALT key
#define VK_PAUSE           19    //PAUSE key
#define VK_CAPITAL         20    //CAPS LOCK key
#define VK_ESCAPE          27    //ESC key
#define VK_SPACE           32    //SPACEBAR
#define VK_PRIOR           33    //PAGE UP key
#define VK_NEXT            34    //PAGE DOWN key
#define VK_END             35    //END key
#define VK_HOME            36    //HOME key
#define VK_LEFT            37    //LEFT ARROW key
#define VK_UP              38    //UP ARROW key
#define VK_RIGHT           39    //RIGHT ARROW key
#define VK_DOWN            40    //DOWN ARROW key
#define VK_PRINT           42    //PRINT key
#define VK_SNAPSHOT        44    //PRINT SCREEN key
#define VK_INSERT          45    //INS key
#define VK_DELETE          46    //DEL key
#define VK_HELP            47    //HELP key
#define VK_LWIN            91    //Left Windows key (Microsoft® Natural® keyboard)
#define VK_RWIN            92    //Right Windows key (Natural keyboard)
#define VK_APPS            93    //Applications key (Natural keyboard)
#define VK_SLEEP           95    //Computer Sleep key
#define VK_NUMPAD0         96    //Numeric keypad 0 key
#define VK_NUMPAD1         97    //Numeric keypad 1 key
#define VK_NUMPAD2         98    //Numeric keypad 2 key
#define VK_NUMPAD3         99    //Numeric keypad 3 key
#define VK_NUMPAD4         100   //Numeric keypad 4 key
#define VK_NUMPAD5         101   //Numeric keypad 5 key
#define VK_NUMPAD6         102   //Numeric keypad 6 key
#define VK_NUMPAD7         103   //Numeric keypad 7 key
#define VK_NUMPAD8         104   //Numeric keypad 8 key
#define VK_NUMPAD9         105   //Numeric keypad 9 key
#define VK_MULTIPLY        106   //Multiply key
#define VK_ADD             107   //Add key
#define VK_SEPARATOR       108   //Separator key
#define VK_SUBTRACT        109   //Subtract key
#define VK_DECIMAL         110   //Decimal key
#define VK_DIVIDE          111   //Divide key
#define VK_F1              112   //F1 key
#define VK_F2              113   //F2 key
#define VK_F3              114   //F3 key
#define VK_F4              115   //F4 key
#define VK_F5              116   //F5 key
#define VK_F6              117   //F6 key
#define VK_F7              118   //F7 key
#define VK_F8              119   //F8 key
#define VK_F9              120   //F9 key
#define VK_F10             121   //F10 key
#define VK_F11             122   //F11 key
#define VK_F12             123   //F12 key
#define VK_F13             124   //F13 key
#define VK_NUMLOCK         144   //NUM LOCK key
#define VK_SCROLL          145   //SCROLL LOCK key
#define VK_LSHIFT          160   //Left SHIFT key
#define VK_RSHIFT          161   //Right SHIFT key
#define VK_LCONTROL        162   //Left CONTROL key
#define VK_RCONTROL        163   //Right CONTROL key
#define VK_LMENU           164   //Left MENU key
#define VK_RMENU           165   //Right MENU key

int Orders;
string bh       = "afxBtnTradeBH";
string brtf     = "afxBtnTradeBRTF";
string bi       = "afxBtnTradeBI";
string bo       = "afxBtnTradeBO";
string sh       = "afxBtnTradeSH";
string srtf     = "afxBtnTradeSRTF";
string si       = "afxBtnTradeSI";
string so       = "afxBtnTradeSO";

//+------------------------------------------------------------------+
//| Allow the sending of a button                                    |
//+------------------------------------------------------------------+
//
void SendKey(int key, bool release = false) {  
        Print("KeyDown");    
    keybd_event(VK_MENU, 0, 0, 0);                                      // Ctrl/alt key down
    keybd_event(key, 0, 0, 0);                                          // key down
    Sleep(2000);
    keybd_event(key, 0, KEYEVENTF_KEYUP, 0);            // key up
    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);        // ctrl/alt key up
        Print("KeyUp");    
}

//+------------------------------------------------------------------+
//| Create the button                                                |
//+------------------------------------------------------------------+
//
bool ButtonCreate(string name, int x, int y, int w, int h, string text) {
        ResetLastError();
        if(!ObjectCreate(ChartID(),name,OBJ_BUTTON,0,0,0)) {
                Print(__FUNCTION__,
                ": failed to create the button! Error code = ",GetLastError());
                return(false);
        }
        ObjectSetInteger(ChartID(),name,OBJPROP_XDISTANCE,x);
        ObjectSetInteger(ChartID(),name,OBJPROP_YDISTANCE,y);
        ObjectSetInteger(ChartID(),name,OBJPROP_XSIZE,w);
        ObjectSetInteger(ChartID(),name,OBJPROP_YSIZE,h);
        ObjectSetInteger(ChartID(),name,OBJPROP_CORNER,CORNER_RIGHT_UPPER);  //--- set the chart's corner, relative to which point coordinates are defined
        ObjectSetString(ChartID(),name,OBJPROP_TEXT,text);
        ObjectSetString(ChartID(),name,OBJPROP_FONT,"Tahoma");
        ObjectSetInteger(ChartID(),name,OBJPROP_FONTSIZE,7);
        ObjectSetInteger(ChartID(),name,OBJPROP_COLOR,clrBlack);
        ObjectSetInteger(ChartID(),name,OBJPROP_BGCOLOR,clrGainsboro);
        ObjectSetInteger(ChartID(),name,OBJPROP_BORDER_COLOR,clrNONE);
        ObjectSetInteger(ChartID(),name,OBJPROP_BACK,false);                            //--- display fg(false) or bg (true)
        ObjectSetInteger(ChartID(),name,OBJPROP_STATE,false);                           //--- set button state
        ObjectSetInteger(ChartID(),name,OBJPROP_SELECTABLE,false);
        ObjectSetInteger(ChartID(),name,OBJPROP_SELECTED,false);
        ObjectSetInteger(ChartID(),name,OBJPROP_HIDDEN,false);                          //--- hide (true) or display (false) graphical object name in the object list
        ObjectSetInteger(ChartID(),name,OBJPROP_ZORDER,0);                                      //--- set the priority for receiving the event of a mouse click in the chart

        return(true);
}
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
        Print("AFX Deinit reason: ", reason);
        ObjectsDeleteAll(0,"afx");      
        ObjectDelete(ChartID(),"noteStatusLabel");      
        ObjectDelete(ChartID(),"noteHeaderLabel");
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
        ObjectDelete(0,"afx");  
        if(!ButtonCreate(bh, btnX+(btnW*0), btnY, btnW, btnH, "Buy On H+")) {
                return(90);
        }
        if(!ButtonCreate(brtf, btnX+(btnW*1), btnY, btnW, btnH, "Buy On H+")) {
                return(90);
        }
        if(!ButtonCreate(bi, btnX+(btnW*2), btnY, btnW, btnH, "Buy Inside")) {
                return(90);
        }
        if(!ButtonCreate(bo, btnX+(btnW*3), btnY, btnW, btnH, "Buy Outside")) {
                return(90);
        }
     
        if(!ButtonCreate(sh, btnX+(btnW*0), btnY+btnH+btnYSpacer, btnW, btnH, "Sell On H+")) {
                return(90);
        }
        if(!ButtonCreate(srtf, btnX+(btnW*1), btnY+btnH+btnYSpacer, btnW, btnH, "Sell On R/TF+")) {
                return(90);
        }
        if(!ButtonCreate(si, btnX+(btnW*2), btnY+btnH+btnYSpacer, btnW, btnH, "Sell Inside")) {
                return(90);
        }
        if(!ButtonCreate(so, btnX+(btnW*3), btnY+btnH+btnYSpacer, btnW, btnH, "Sell Outside")) {
                return(90);
        }

        return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {

        return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {
   
        if(sparam==bh) {                        // Alt-H
                SendKey(VK_H,0);
        } else if(sparam==brtf) {       // Alt-T
                SendKey(VK_T,0);
        } else if(sparam==bi) {         // Alt-I
                SendKey(VK_I,0);
        } else if(sparam==bo) {         // Alt-O
                SendKey(VK_O,0);
        } else if(sparam==sh) {         // Alt-N
                SendKey(VK_N,0);
        } else if(sparam==srtf) {       // Alt-G
                SendKey(VK_G,0);
        } else if(sparam==si) {         // Alt-K
                SendKey(VK_K,0);
        } else if(sparam==so) {         // Alt-L
                SendKey(VK_L,0);
        }
}
 
strontiumDog:

As you can see the SendKey function has the keydown and keyup so Windows/mql4 setup operates the hotkey button ok function-wise.  But on my chart, the button stays depressed.  I can click it again, and the script attached runs ok again, and the button undepresses, but this is not the functionality I want - obviously I was hoping to have the button undepress itself.
 
Where in your code do you re-set the button state?
 
GumRai:
Where in your code do you re-set the button state?

Well after the sendkey statements I had the following, but the effect that had was to make it look like the button did not 'press'.  The effect of clicking it worked, but it didnt press and unpress, like a normal OS button.  Where should I put the ObjectSetInteger statement so it has the desired effect?


ObjectSetInteger(0,bh,OBJPROP_STATE,false)
 

Here, the code is supposed to pause and spread the actions out over time so I can see what is going on.

But the button doesn't ever look depressed, and the sleep calls are ignored.  Anyone know why this all doesn't work?

How should I manage a good user experience regarding depressing and undepressing buttons?


void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {
   
        if(sparam==bh) {                        // Alt-H
                Sleep(5000);
                ObjectSetInteger(0,bh,OBJPROP_STATE,true);
                Sleep(5000);
                SendKey(VK_H,0);
                Sleep(5000);
                ObjectSetInteger(0,bh,OBJPROP_STATE,false);
        } else if(sparam==brtf) {       // Alt-T
                SendKey(VK_T,0);
                ObjectSetInteger(0,brtf,OBJPROP_STATE,false);
        } else if(sparam==bi) {         // Alt-I
                SendKey(VK_I,0);
                ObjectSetInteger(0,bi,OBJPROP_STATE,false);
        } else if(sparam==bo) {         // Alt-O
                SendKey(VK_O,0);
                ObjectSetInteger(0,bo,OBJPROP_STATE,false);
        } else if(sparam==sh) {         // Alt-N
                SendKey(VK_N,0);
        } else if(sparam==srtf) {       // Alt-G
                SendKey(VK_G,0);
        } else if(sparam==si) {         // Alt-K
                SendKey(VK_K,0);
        } else if(sparam==so) {         // Alt-L
                SendKey(VK_L,0);
        }
}
 
strontiumDog:

Here, the code is supposed to pause and spread the actions out over time so I can see what is going on.

But the button doesn't ever look depressed, and the sleep calls are ignored.  Anyone know why this all doesn't work?

How should I manage a good user experience regarding depressing and undepressing buttons?


Right I see Sleep is ignored in Indicators.  I'll dig further and see where I can get a result with this.
 
Whatever you try to do it wont work with doubleclicks (or just too quick clicks) anyway.