Skip to content

How to handle mouse buttons and key presses?

VBrazhnik edited this page Jun 17, 2019 · 4 revisions

On macOS, if you want to handle mouse buttons and key presses and close the window with the red button, you can use the following function instead of the other hook functions:

int mlx_hook(void *win_ptr, int x_event, int x_mask, int (*funct)(), void *param);

To handle a key press

At the place of int x_event parametr use 2.

At the place of int (*funct)() parameter you use the following function:

int key_press(int keycode, void *param)

To handle a key release

At the place of int x_event parametr use 3.

At the place of int (*funct)() parameter you use the following function:

int key_release(int keycode, void *param)

To handle a mouse button press

At the place of int x_event parametr use 4.

At the place of int (*funct)() parameter you use the following function:

int mouse_press(int button, int x, int y, void *param)

To handle a mouse button release

At the place of int x_event parametr use 5.

At the place of int (*funct)() parameter you use the following function:

int mouse_release(int button, int x, int y, void *param)

To handle a mouse movement

At the place of int x_event parametr use 6.

At the place of int (*funct)() parameter you use the following function:

int mouse_move(int x, int y, void *param)

To handle an expose event

At the place of int x_event parametr use 12.

At the place of int (*funct)() parameter you use the following function:

int expose(void *param)

To handle a red button (X button) press

At the place of int x_event parametr use 17.

At the place of int (*funct)() parameter you use the following function:

int close(void *param)

Complete int close(void *param) function:

int close(void *param)
{
    (void)param;
    exit(0);
}

Tip:

x_mask is ignored on macOS. But if you want that your FdF will have compatibility with Linux, you must use x_mask.

Key codes

key_codes

Mouse button codes

  • Left button — 1

  • Right button — 2

  • Third (Middle) button — 3

  • Scroll Up — 4

  • Scroll Down — 5

  • Scroll Left — 6

  • Scroll Right — 7

Masks

You can find values of x_mask here.

Tip:

x_mask for int close(void *param) is (1L << 17).