Creating a mouse click simulation, in windows development

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Creating a mouse click simulation, in windows development

      For development, I am trying to simulate a mouse click on emWIn Windows simulation.
      It works fine on the embedded device, but will make it much easier for development if I can simulate on my pC.
      I tried this, but it does not work.

      Source Code

      1. int xpos_ = 0,ypos_ = 0;
      2. int GUI_TOUCH_X_MeasureX(void) {
      3. return xpos_;
      4. }
      5. int GUI_TOUCH_X_MeasureY(void) {
      6. return ypos_;
      7. }
      8. void doMouseCmd()
      9. {
      10. xpos_ = getXpos();
      11. ypos_ = getYpos();
      12. printf("Processing Mouse cmmd x= %d y = %d\n\n\n",xpos_,ypos_);
      13. GUI_TOUCH_Exec(); // get x
      14. GUI_TOUCH_Exec(); // get y
      15. }
      Display All
      Any idea how to accomplish this?
      Thanks.
    • Hi,

      Try using the function GUI_PID_StoreState(). Simply fill the GUI_PID_STATE structure, passed along with this function, with the desired coordinates and if a press or unpress event occurs.

      For a click the unpress event should happen on the same position as the last down event.

      Below is an example, on click or release of the upper button a GUI_PID_STATE structure gets filled and passed to emWin. Since the up and down events occur inside the second button, it gets pressed and released.

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Static code
      5. *
      6. **********************************************************************
      7. */
      8. /*********************************************************************
      9. *
      10. * _cbBk
      11. */
      12. static void _cbBk(WM_MESSAGE * pMsg) {
      13. int Id;
      14. int NCode;
      15. static GUI_PID_STATE State;
      16. switch (pMsg->MsgId) {
      17. case WM_PAINT:
      18. GUI_SetBkColor(GUI_BLACK);
      19. GUI_Clear();
      20. break;
      21. case WM_NOTIFY_PARENT:
      22. Id = WM_GetId(pMsg->hWinSrc);
      23. NCode = pMsg->Data.v;
      24. if (Id == GUI_ID_BUTTON0) {
      25. switch (NCode) {
      26. case WM_NOTIFICATION_CLICKED:
      27. State.x = 50; // x middle of the other button
      28. State.y = 50; // y middle of the other button
      29. State.Pressed = 1;
      30. GUI_PID_StoreState(&State);
      31. break;
      32. case WM_NOTIFICATION_RELEASED:
      33. //
      34. // x and y do not change but the pressed state
      35. //
      36. State.Pressed = 0;
      37. GUI_PID_StoreState(&State);
      38. break;
      39. }
      40. }
      41. break;
      42. default:
      43. WM_DefaultProc(pMsg);
      44. break;
      45. }
      46. }
      47. /*********************************************************************
      48. *
      49. * Public code
      50. *
      51. **********************************************************************
      52. */
      53. /*********************************************************************
      54. *
      55. * MainTask
      56. */
      57. void MainTask(void) {
      58. WM_HWIN hItem;
      59. GUI_Init();
      60. //
      61. // Set callback to manage the background
      62. //
      63. WM_SetCallback(WM_HBKWIN, _cbBk);
      64. //
      65. // This button causes a simulated mouse click on the second button
      66. //
      67. hItem = BUTTON_CreateEx(10, 10, 80, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_BUTTON0);
      68. BUTTON_SetText(hItem, "Click me");
      69. //
      70. // This button will get pressed when pressing the other button.
      71. //
      72. BUTTON_CreateEx(10, 40, 80, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_BUTTON1);
      73. while (1) {
      74. GUI_Delay(100);
      75. }
      76. }
      Display All

      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Thanks Sven,
      I tried your code and it works OK with simulation, but my code does not.
      Should I call GUI_PID_StoreState() only inside the callBack function?

      My code is simple but in another thread (FreeRtos, but only one task calling GUI_EXE)
      any idea why it does not work?

      Source Code

      1. void doMouseCmd( char * dPtr)
      2. {
      3. GUI_PID_STATE State;
      4. // 4 bytes are x, 4 bytes are y
      5. State.x = ptrToInt(dPtr,2);
      6. State.y = ptrToInt(dPtr+4,2);
      7. printf("\nProcessing Mouse cmmd x= %d y = %d\n",State.x,State.y);
      8. State.Pressed = 1;
      9. GUI_PID_StoreState(&State);
      10. State.Pressed = 0;
      11. GUI_PID_StoreState(&State);
      12. }
      Display All
      Regards,
    • Hi,

      You are passing a pressed and unpressed event directly after each other. On my end this is working but it will look like nothing happens. The only thing visible is that the button gets the focus.

      Try to perform a delay between the pressed and unpressed change in your other thread (not a GUI_Delay() one of your OS).
      Then it should become visible. Like in the code below.

      Also try to set breakpoints on both 'NCode = 0' in _cbBk(). Then you should run on them. First on the clicked and then on released event.

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Static code
      5. *
      6. **********************************************************************
      7. */
      8. /*********************************************************************
      9. *
      10. * _cbBk
      11. */
      12. static void _cbBk(WM_MESSAGE * pMsg) {
      13. int Id;
      14. int NCode;
      15. switch (pMsg->MsgId) {
      16. case WM_PAINT:
      17. GUI_SetBkColor(GUI_BLACK);
      18. GUI_Clear();
      19. break;
      20. case WM_NOTIFY_PARENT:
      21. Id = WM_GetId(pMsg->hWinSrc);
      22. NCode = pMsg->Data.v;
      23. if (Id == GUI_ID_BUTTON0) {
      24. switch (NCode) {
      25. case WM_NOTIFICATION_CLICKED:
      26. NCode = 0;
      27. break;
      28. case WM_NOTIFICATION_RELEASED:
      29. NCode = 0;
      30. break;
      31. }
      32. }
      33. break;
      34. default:
      35. WM_DefaultProc(pMsg);
      36. break;
      37. }
      38. }
      39. /*********************************************************************
      40. *
      41. * _DoMouse
      42. */
      43. static void _DoMouse(void) {
      44. GUI_PID_STATE State;
      45. State.x = 50;
      46. State.y = 20;
      47. State.Layer = 0;
      48. State.Pressed = 1;
      49. GUI_PID_StoreState(&State);
      50. YourRTOS_Delay(1000);
      51. State.Pressed = 0;
      52. GUI_PID_StoreState(&State);
      53. }
      54. /*********************************************************************
      55. *
      56. * Public code
      57. *
      58. **********************************************************************
      59. */
      60. /*********************************************************************
      61. *
      62. * MainTask
      63. */
      64. void MainTask(void) {
      65. WM_HWIN hItem;
      66. GUI_Init();
      67. //
      68. // Set callback to manage the background
      69. //
      70. WM_SetCallback(WM_HBKWIN, _cbBk);
      71. //
      72. // This button causes a simulated mouse click on the second button
      73. //
      74. hItem = BUTTON_CreateEx(10, 10, 80, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_BUTTON0);
      75. BUTTON_SetText(hItem, "Click me");
      76. while (1) {
      77. GUI_Delay(1000);
      78. _DoMouse();
      79. }
      80. }
      Display All
      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Working !!
      Thanks very much.
      I use FREERtos queue inside the call back routine, I find this to be stable, if it helps anyone:

      C Source Code

      1. static void cbScreen(WM_MESSAGE * pMsg) {
      2. int i;
      3. char * barMsg;
      4. TGuiQdata guiMSg ;
      5. WM_HWIN hItemNear;
      6. GUI_PID_STATE remMouse;
      7. GUI_PID_STATE * pState;
      8. if(remoteMouseQue != NULL)
      9. {
      10. if(xQueueReceive(remoteMouseQue, &remMouse , 0) )
      11. {
      12. remMouse.Pressed = 1;
      13. GUI_PID_StoreState(&remMouse);
      14. // vTaskDelay(10);
      15. remMouse.Pressed = 0;
      16. GUI_PID_StoreState(&remMouse);
      17. }
      18. }
      19. switch (pMsg->MsgId)
      20. {
      21. case WM_INIT_DIALOG:
      22. initScreenObjects(pMsg);
      23. .
      24. .
      25. .
      Display All


      Johanan

      The post was edited 1 time, last by johanan ().