Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

When are WndProc events called?

Discussion in 'Windows' started by Ewanuk, Mar 16, 2020.

  1. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    We have a windows-standalone application in the works, and we've hooked up some native WndProc message handling. What I'm trying to figure out is when these events get fired in relation to unity's main thread, or even if it's on another thread altogether (which seems unlikely since I haven't run into any threading issues yet)

    If say I have this going:


    Code (CSharp):
    1. [DllImport("user32.dll")]
    2. static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    3.  
    4.  
    5.         public WndProcInterceptor(IntPtr hwndToIntercept)
    6.         {
    7.             this.hwndToIntercept = hwndToIntercept;
    8.  
    9.             newWndProc = new WndProcDelegate(CustomWndProc);
    10.             ptrToMyHandler = Marshal.GetFunctionPointerForDelegate(newWndProc);
    11.             ptrToOriginalHandler = SetWindowLongPtr(hwndToIntercept, -4, ptrToMyHandler);
    12.         }
    13.  
    14.         IntPtr CustomWndProc(IntPtr hWnd, uint msgAsInt, IntPtr wParam, IntPtr lParam)
    15.         {
    16.             WindowsMessage msg = (WindowsMessage)msgAsInt;
    17.             Window context = WindowManager.Singleton.GetWindow();
    18.             if (context == null) { return CallWindowProc(ptrToOriginalHandler, hWnd, msgAsInt, wParam, lParam); }
    19.  
    20.             switch(msg)
    21.             {
    22.                     case WindowsMessage.WM_LBUTTONDOWN:
    23.                     {
    24.                         context.UIInput.SetMouseDown(0, true);
    25.                         break;
    26.                     }
    27.                     case WindowsMessage.WM_LBUTTONUP:
    28.                     {
    29.                         context.UIInput.SetMouseDown(0, false);
    30.                         break;
    31.                     }
    32.                     case WindowsMessage.WM_RBUTTONDOWN:
    33.                     {
    34.                         context.UIInput.SetMouseDown(1, true);
    35.                         break;
    36.                     }
    37.                     case WindowsMessage.WM_RBUTTONUP:
    38.                     {
    39.                         context.UIInput.SetMouseDown(1, false);
    40.                         break;
    41.                     }
    42.                     case WindowsMessage.WM_MOUSEMOVE:
    43.                     {
    44.                         int x = WindowsMessageUtil.GetMouseEventXCoord(lParam);
    45.                         int y = WindowsMessageUtil.GetMouseEventYCoord(lParam);
    46.  
    47.                         // Have to flip y-coordinate, windows origin is top-left, unity is bottom-left
    48.                         y = context.Height - y;
    49.                         context.UIInput.SetMousePos(x, y);
    50.  
    51.                         break;
    52.                     }
    53.                     default:
    54.                         break;
    55.             }
    56.          
    57.             // Allow event to propogate to the original handler if it gets that far
    58.             return CallWindowProc(ptrToOriginalHandler, hWnd, msgAsInt, wParam, lParam);
    59.         }
    When can I expect messages to get executed in relation to https://docs.unity3d.com/Manual/ExecutionOrder.html

    I'm guessing unity polls all the messages at the input step? Between frames?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    They will arrive very early in the frame, before anything else happens. These events will come on the main thread.

    Note, if you want to handle input events, you should be listening for WM_INPUT messages instead.
     
  3. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    Awesome, thanks!