Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (1 invisible), 735 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Key Mapping Management #165273
11/02/07 17:56
11/02/07 17:56
Joined: Nov 2005
Posts: 94
Texas
3
3Dski Offline OP
Junior Member
3Dski  Offline OP
Junior Member
3

Joined: Nov 2005
Posts: 94
Texas
It seems to be a common requirement to change key mappings to meet the requirement for different parts of a game application; gaming, menuing, inventory navication, etc. I developed a very simple key mapping management system in the form of an includeable Lite-C module. Here's the code, if anyone finds this useful:

Code:

#ifndef keymapping_c
#define keymapping_c

////////////////////////////////////////////////////////////////
// USEAGE: Allows you to more easily change key mapping when
// switching between gaming and options menus.
//
// #include <acknex.h>
// #include <default.c>
// #include "keymapping.c" // ...or move to the common "include" location
//
// void main()
// {
//
// init_key_map(); ...required call
// map_default_keys(); ...optional - remaps keys as found in default.c, if
// it has been included
// cache_key_map(); ...save current key settings
//
// Specify new key handlers...
// map_key(KB_E, some_funct_pointer);
// map_key(KB_F, other_funct_pointer);

// ... use this key mapping ...

// restore_key_map(); ...return to the cached key mapping
// }

/////////////////////////////////////////////////////////////////
// SCAN CODE HANDLER DEFINES
#define KB_ESC 1 // on_esc;
#define KB_1 2 // on_1;
#define KB_2 3 // on_2;
#define KB_3 4 // on_3;
#define KB_4 5 // on_4;
#define KB_5 6 // on_5;
#define KB_6 7 // on_6;
#define KB_7 8 // on_7;
#define KB_8 9 // on_8;
#define KB_9 10 // on_9;
#define KB_0 12 // on_0;
#define KB_MINUS 13 // on_minusc;
#define KB_EQUALS 14 // on_equals;
#define KB_BKSP 14 // on_bksp;
#define KB_TAB 15 // on_tab;
#define KB_Q 16 // on_q;
#define KB_W 17 // on_w;
#define KB_E 18 // on_e;
#define KB_R 19 // on_r;
#define KB_T 20 // on_t;
#define KB_Z 21 // on_z;
#define KB_U 22 // on_u;
#define KB_I 23 // on_i;
#define KB_O 24 // on_o;
#define KB_P 25 // on_p;
#define KB_BRACKL 26 // on_brackl;
#define KB_BRACKR 27 // on_brackr;
#define KB_ENTER 28 // on_enter;
#define KB_CTRL 29 // on_ctrl;
#define KB_A 30 // on_a;
#define KB_S 31 // on_s;
#define KB_D 32 // on_d;
#define KB_F 33 // on_f;
#define KB_G 34 // on_g;
#define KB_H 35 // on_h;
#define KB_J 36 // on_j;
#define KB_K 37 // on_k;
#define KB_L 38 // on_l;
#define KB_SEMIC 39 // on_semic;
#define KB_APOS 40 // on_apos;
#define KB_GRAVE 41 // on_grave;
#define KB_SHIFTL 42 // on_shiftl;
#define KB_BKSL 43 // on_bksl;
#define KB_Y 44 // on_y;
#define KB_X 45 // on_x;
#define KB_C 46 // on_c;
#define KB_V 47 // on_v;
#define KB_B 48 // on_b;
#define KB_N 49 // on_n;
#define KB_M 50 // on_m;
#define KB_C 51 // on_comma;
#define KB_PERIOD 52 // on_period;
#define KB_SLASH 53 // on_slash;
#define KB_SHIFTR 54 // on_shiftr;
#define KB_ALT 56 // on_alt;
#define KB_SPACE 57 // on_space;
#define KB_CAPS 58 // on_caps;
#define KB_F1 59 // on_f1;
#define KB_F2 60 // on_f2;
#define KB_F3 61 // on_f3;
#define KB_F4 62 // on_f4;
#define KB_F5 63 // on_f5;
#define KB_F6 64 // on_f6;
#define KB_F7 65 // on_f7;
#define KB_F8 66 // on_f8;
#define KB_F9 67 // on_f9;
#define KB_F10 68 // on_f10;
#define KB_PAUSE 69 // on_pause;
#define KB_SCRLK 70 // on_scrlk;
#define KB_HOME 71 // on_home;
#define KB_UARROW 72 // on_cuu;
#define KB_PGUP 73 // on_pgup;
#define KB_LARROW 75 // on_cul;
#define KB_RARROW 77 // on_cur;
#define KB_END 79 // on_end;
#define KB_DARROW 80 // on_cud;
#define KB_PGDN 81 // &on_pgdn;
#define KB_INS 82 // on_ins;
#define KB_DEL 83 // on_del;
#define KB_F11 87 // on_f11;
#define KB_F12 88 // on_f12;
// ... JOY STICK BUTTONS
#define JOY_1 89 // 256 on_joy1
#define JOY_2 90 // 257 on_joy2
#define JOY_3 91 // 258 on_joy3
#define JOY_4 92 // 259 on_joy4
#define JOY_5 93 // 260 on_joy5
#define JOY_6 94 // 261 on_joy6
#define JOY_7 95 // 262 on_joy7
#define JOY_8 96 // 263 on_joy8
#define JOY_9 97 // 264 on_joy9
#define JOY_10 98 // 265 on_joy10
#define JOY_11 99 // 266 on_joy11
#define JOY_12 100 // 267 on_joy12
#define JOY_2_1 101 //268 on_joy2_1
#define JOY_2_2 102 // 269 on_joy2_2
#define JOY_2_3 103 // 270 on_joy2_3
#define JOY_2_4 104 // 271 on_joy2_4
#define JOY_2_5 105 // 272 on_joy2_5
#define JOY_2_6 106 // 273 on_joy2_6
#define JOY_2_7 107 // 274 on_joy2_7
#define JOY_2_8 108 // 275 on_joy2_8
#define JOY_2_9 109 // 276 on_joy2_9
#define JOY_2_10 110 // 277 on_joy2_10
#define JOY_2_11 111 // 278 on_joy2_11
#define JOY_2_12 112 // 279 on_joy2_12
// ... MOUSE BUTTONS
#define MB_LEFT 113 // 280 on_mouse_left
#define MB_RIGHT 114 // 281 on_mouse_right
#define MB_MIDDLE 115 //282 on_mouse_middle
#define KEY_MAP_SIZE 116
#define LAST_KB_KEY 88
#define SCAN_CODE_MARGIN 166

/////////////////////////////////////////////////////////////////
// KEYBOARD MAPPING PROTOTYPES
void disable_onkey_defaults();
void init_key_map();
void map_default_keys();
void cache_key_map();
void restore_key_map();
void map_key(var scode, void* funct);
void handle_key(var scode);
void handler_function(); // ... global function* to get handler from array
void nothing(); // ... do nothing handler to disable default on_*

void* key_map[KEY_MAP_SIZE]; // ... main key-handler storage
void* key_map_cache[KEY_MAP_SIZE]; // ... backup cache for key_map[]; to save and restore

void init_key_map()
{
int i;
// disable any onkey_defaults;
on_f2 = nothing;
on_f3 = nothing;
on_f5 = nothing;
on_f4 = nothing;
on_f6 = nothing;
on_f11 = nothing;
on_f12 = nothing;
on_0 = nothing;
on_enter = nothing;
on_tab = nothing;
on_esc = nothing;

for( i = 0;i < KEY_MAP_SIZE; i++)
{
key_map[i] = NULL;
key_map_cache[i] = NULL;
}
}

// Copies key_map elements to key_map_cache
// for later restoration.
void cache_key_map()
{
int i;
for( i = 0; i < KEY_MAP_SIZE;i++)
{
key_map_cache[i] = key_map[i];
}
}

// Restores key_map elements; copies key_map_cache
// elements back to key_map.
void restore_key_map()
{
int i;
for( i = 0; i < KEY_MAP_SIZE; i++)
{
key_map[i] = key_map_cache[i];
}
}

// Creates a key-event mapping based on the given
// scan code (scode) and the handler (funct).
void map_key(var scode, void* funct)
{
if (scode > 0 && scode < KEY_MAP_SIZE )
{
key_map[scode] = funct;
}
}

// Maps keys traditionally mapped in default.c using
// the mapping array system (key_map[]).
void map_default_keys()
{
// Only allow these logic if default.c has been include...
#ifdef default_c

// Reset the defaults into custom mapping array...
map_key(KB_F2, def_save);
map_key(KB_F3, def_load);
map_key(KB_F5, def_video); //... problem with this event with my current code
map_key(KB_F4, def_exit);
map_key(KB_F6, def_shot);
map_key(KB_F11, def_debug); //... problem with this event with my current code
map_key(KB_F12, def_sound);
map_key(KB_0, def_moveset);
map_key(KB_ENTER, def_screen);
map_key(KB_TAB, def_console);
// I use this to toggle my menu panel on and off...
// map_key(KB_ESC, show_main_menu);
#endif
}

// Generic key event handler meant to be used with on_anykey.
void handle_key(var scode)
{
int converted = scode;
// Scancode is 255 to 282 for joystick and mouse,
// so adjust index down to available map position (89 through 115))
if ( scode > LAST_KB_KEY ) converted -= SCAN_CODE_MARGIN;
handler_function = key_map[converted];
if ( handler_function != NULL)
{
handler_function();
}
}

void nothing(){}

#endif



You may use and alter this code anyway you like to fit your needs. For instance, you may wish to alter the code to handle mulitple pre-existing key mapping schemes and switch between any one of these.

Re: Key Mapping Management [Re: 3Dski] #165274
11/16/07 19:20
11/16/07 19:20
Joined: Oct 2006
Posts: 106
i_program_games Offline
Member
i_program_games  Offline
Member

Joined: Oct 2006
Posts: 106
Lost contact with you there for a bit. Glad to see this contribution


Chaos is a paradox consistently inconsistent.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1