Skip to content

Commit

Permalink
feat: 增加鼠标扩展键的检测
Browse files Browse the repository at this point in the history
  • Loading branch information
yixy-only committed Oct 28, 2024
1 parent e004364 commit daf4791
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 23 deletions.
16 changes: 13 additions & 3 deletions include/ege.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ enum key_code_e
key_mouse_l = 0x01,
key_mouse_r = 0x02,
key_mouse_m = 0x04,
key_mouse_x1 = 0x05,
key_mouse_x2 = 0x06,
key_back = 0x08,
key_tab = 0x09,
key_enter = 0x0d,
Expand Down Expand Up @@ -722,9 +724,11 @@ enum mouse_msg_e

enum mouse_flag_e
{
mouse_flag_left = 1,
mouse_flag_right = 2,
mouse_flag_mid = 4,
mouse_flag_left = 0x001,
mouse_flag_right = 0x002,
mouse_flag_mid = 0x004,
mouse_flag_x1 = 0x008,
mouse_flag_x2 = 0x010,
mouse_flag_shift = 0x100,
mouse_flag_ctrl = 0x200
};
Expand All @@ -736,9 +740,13 @@ struct mouse_msg
mouse_msg_e msg;
unsigned int flags;
int wheel;

bool is_left() const {return (flags & mouse_flag_left) != 0;}
bool is_right() const {return (flags & mouse_flag_right) != 0;}
bool is_mid() const {return (flags & mouse_flag_mid) != 0;}
bool is_x1() const {return (flags & mouse_flag_x1) != 0;}
bool is_x2() const {return (flags & mouse_flag_x2) != 0;}

bool is_down() const {return msg == mouse_msg_down;}
bool is_up() const {return msg == mouse_msg_up;}
bool is_move() const {return msg == mouse_msg_move;}
Expand All @@ -753,6 +761,8 @@ struct MOUSEMSG
bool mkLButton;
bool mkMButton;
bool mkRButton;
bool mkX1Button;
bool mkX2Button;
short x;
short y;
short wheel;
Expand Down
2 changes: 1 addition & 1 deletion src/ege_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ struct _graph_setting
HANDLE threadui_handle;

/* 鼠标状态记录 */
int mouse_state_l, mouse_state_m, mouse_state_r;
int mouse_state_l, mouse_state_m, mouse_state_r, mouse_state_x1, mouse_state_x2;
int mouse_last_x, mouse_last_y;
int mouse_lastclick_x, mouse_lastclick_y;
int mouse_lastup_x, mouse_lastup_y;
Expand Down
47 changes: 43 additions & 4 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ static void push_mouse_msg(struct _graph_setting* pg, UINT message, WPARAM wpara
msg.message = message;
msg.wParam = wparam;
msg.lParam = lparam;
msg.mousekey = (pg->mouse_state_m << 2) | (pg->mouse_state_r << 1) | (pg->mouse_state_l << 0);
msg.mousekey = (pg->mouse_state_m << 2) | (pg->mouse_state_r << 1) | (pg->mouse_state_l << 0)
| (pg->mouse_state_x1 << 3) | (pg->mouse_state_x1 << 4);
msg.time = ::GetTickCount();
pg->msgmouse_queue->push(msg);
}
Expand Down Expand Up @@ -559,12 +560,29 @@ static LRESULT CALLBACK wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
push_mouse_msg(pg, message, wParam, lParam);
}
break;
case WM_XBUTTONDOWN:
case WM_XBUTTONDBLCLK:
pg->mouse_lastclick_x = (short int)((UINT)lParam & 0xFFFF);
pg->mouse_lastclick_y = (short int)((UINT)lParam >> 16);

if ((wParam >> 16) & 0x0001) {
pg->keystatemap[VK_XBUTTON1] = 1;
} else if ((wParam >> 16) & 0x0002){
pg->keystatemap[VK_XBUTTON2] = 1;
}
SetCapture(hWnd);
pg->mouse_state_x1 = 1;
if (hWnd == pg->hwnd) {
push_mouse_msg(pg, message, wParam, lParam);
}
break;
case WM_LBUTTONUP:
pg->mouse_lastup_x = (short int)((UINT)lParam & 0xFFFF);
pg->mouse_lastup_y = (short int)((UINT)lParam >> 16);
pg->mouse_state_l = 0;
pg->keystatemap[VK_LBUTTON] = 0;
if (pg->mouse_state_l == 0 && pg->mouse_state_m == 0 && pg->mouse_state_r == 0) {
if (pg->mouse_state_l == 0 && pg->mouse_state_m == 0 && pg->mouse_state_r == 0
&& pg->mouse_state_x1 && pg->mouse_state_x2) {
ReleaseCapture();
}
if (hWnd == pg->hwnd) {
Expand All @@ -576,7 +594,8 @@ static LRESULT CALLBACK wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
pg->mouse_lastup_y = (short int)((UINT)lParam >> 16);
pg->mouse_state_m = 0;
pg->keystatemap[VK_MBUTTON] = 0;
if (pg->mouse_state_l == 0 && pg->mouse_state_m == 0 && pg->mouse_state_r == 0) {
if (pg->mouse_state_l == 0 && pg->mouse_state_m == 0 && pg->mouse_state_r == 0
&& pg->mouse_state_x1 && pg->mouse_state_x2) {
ReleaseCapture();
}
if (hWnd == pg->hwnd) {
Expand All @@ -588,7 +607,27 @@ static LRESULT CALLBACK wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
pg->mouse_lastup_y = (short int)((UINT)lParam >> 16);
pg->mouse_state_r = 0;
pg->keystatemap[VK_RBUTTON] = 0;
if (pg->mouse_state_l == 0 && pg->mouse_state_m == 0 && pg->mouse_state_r == 0) {
if (pg->mouse_state_l == 0 && pg->mouse_state_m == 0 && pg->mouse_state_r == 0
&& pg->mouse_state_x1 && pg->mouse_state_x2) {
ReleaseCapture();
}
if (hWnd == pg->hwnd) {
push_mouse_msg(pg, message, wParam, lParam);
}
break;
case WM_XBUTTONUP:
pg->mouse_lastup_x = (short int)((UINT)lParam & 0xFFFF);
pg->mouse_lastup_y = (short int)((UINT)lParam >> 16);

if ((wParam >> 16) & 0x0001) {
pg->mouse_state_x1 = 0;
pg->keystatemap[VK_XBUTTON1] = 0;
} else if ((wParam >> 16) & 0x0002){
pg->mouse_state_x2 = 0;
pg->keystatemap[VK_XBUTTON2] = 0;
}
if (pg->mouse_state_l == 0 && pg->mouse_state_m == 0 && pg->mouse_state_r == 0
&& pg->mouse_state_x1 && pg->mouse_state_x2) {
ReleaseCapture();
}
if (hWnd == pg->hwnd) {
Expand Down
72 changes: 57 additions & 15 deletions src/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,59 @@ mouse_msg getmouse()
mmsg.x = (short)((int)msg.lParam & 0xFFFF);
mmsg.y = (short)((unsigned)msg.lParam >> 16);
mmsg.msg = mouse_msg_move;
if (msg.message == WM_LBUTTONDOWN) {

switch (msg.message) {
case WM_LBUTTONDOWN:
mmsg.msg = mouse_msg_down;
mmsg.flags |= mouse_flag_left;
} else if (msg.message == WM_RBUTTONDOWN) {
mmsg.msg = mouse_msg_down;
mmsg.flags |= mouse_flag_right;
} else if (msg.message == WM_MBUTTONDOWN) {
mmsg.msg = mouse_msg_down;
mmsg.flags |= mouse_flag_mid;
} else if (msg.message == WM_LBUTTONUP) {
break;
case WM_LBUTTONUP:
mmsg.msg = mouse_msg_up;
mmsg.flags |= mouse_flag_left;
} else if (msg.message == WM_RBUTTONUP) {
break;
case WM_RBUTTONDOWN:
mmsg.msg = mouse_msg_down;
mmsg.flags |= mouse_flag_right;
break;
case WM_RBUTTONUP:
mmsg.msg = mouse_msg_up;
mmsg.flags |= mouse_flag_right;
} else if (msg.message == WM_MBUTTONUP) {
break;
case WM_MBUTTONDOWN:
mmsg.msg = mouse_msg_down;
mmsg.flags |= mouse_flag_mid;
break;
case WM_MBUTTONUP:
mmsg.msg = mouse_msg_up;
mmsg.flags |= mouse_flag_mid;
} else if (msg.message == WM_MOUSEWHEEL) {
break;
case WM_MOUSEWHEEL:
mmsg.msg = mouse_msg_wheel;
mmsg.wheel = (short)((unsigned)msg.wParam >> 16);
break;
case WM_XBUTTONDOWN:
mmsg.msg = mouse_msg_down;

if ((msg.wParam >> 16) & 0x0001) {
mmsg.flags |= mouse_flag_x1;
} else if ((msg.wParam >> 16) & 0x0002) {
mmsg.flags |= mouse_flag_x2;
}

break;
case WM_XBUTTONUP:
mmsg.msg = mouse_msg_up;

if ((msg.wParam >> 16) & 0x0001) {
mmsg.flags |= mouse_flag_x1;
} else if ((msg.wParam >> 16) & 0x0002) {
mmsg.flags |= mouse_flag_x2;
}
break;

default:break;
}

return mmsg;
}
} while (!pg->exit_window && !pg->exit_flag && waitdealmessage(pg));
Expand All @@ -126,15 +157,26 @@ MOUSEMSG GetMouseMsg()
mmsg.mkShift = ((msg.wParam & MK_SHIFT) != 0);
mmsg.x = (short)((int)msg.lParam & 0xFFFF);
mmsg.y = (short)((unsigned)msg.lParam >> 16);
if (msg.mousekey & 1) {
if (msg.mousekey & 0x01) {
mmsg.mkLButton = 1;
}
if (msg.mousekey & 4) {

if (msg.mousekey & 0x02) {
mmsg.mkRButton = 1;
}

if (msg.mousekey & 0x04) {
mmsg.mkMButton = 1;
}
if (msg.mousekey & 2) {
mmsg.mkRButton = 1;

if (msg.mousekey & 0x08) {
mmsg.mkX1Button = 1;
}

if (msg.mousekey & 0x10) {
mmsg.mkX2Button = 1;
}

if (msg.message == WM_MOUSEWHEEL) {
mmsg.wheel = (short)((unsigned)msg.wParam >> 16);
}
Expand Down

0 comments on commit daf4791

Please sign in to comment.