/* =================== Key_Bind_f =================== */ static void Key_Bind_f(void) { int c, b; c = Cmd_Argc(); if (c < 2) { Com_Printf("bind <key> [command] : attach a command to a key\n"); return; } b = Key_StringToKeynum(Cmd_Argv(1)); if (b == -1) { Com_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(1)); return; } if (c == 2) { if (keybindings[b]) Com_Printf("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b]); else Com_Printf("\"%s\" is not bound\n", Cmd_Argv(1)); return; } // copy the rest of the command line Key_SetBinding(b, Cmd_ArgsFrom(2)); }
/** * Simulate press of a key with a command. */ static void CL_PressKey_f (void) { unsigned int keyNum; if (Cmd_Argc() != 2) { Com_Printf("Usage: %s <key> : simulate press of a key\n", Cmd_Argv(0)); return; } keyNum = Key_StringToKeynum(Cmd_Argv(1)); /* @todo unicode value is wrong */ IN_EventEnqueue(keyNum, '?', qtrue); IN_EventEnqueue(keyNum, '?', qfalse); }
/* =================== Cmd_JoyRemap_f =================== */ void Cmd_JoyRemap_f (void) { int c, key; joyevent_t joyevent; int localPlayerNum; c = Cmd_Argc(); if (c < 2) { Com_Printf ("%s <event> [key] : attach a joystick event to a virtual key\n", Cmd_Argv(0)); return; } localPlayerNum = Com_LocalPlayerForCvarName( Cmd_Argv(0) ); if ( playerJoyRemapIndex[ localPlayerNum ] == -1 ) { Com_Printf( "Joystick for player %d not initialized\n", localPlayerNum+1 ); return; } if (!CL_StringToJoyEvent(Cmd_Argv(1), &joyevent)) { Com_Printf ("\"%s\" isn't a valid joystick event\n", Cmd_Argv(1)); return; } if (c == 2) { key = CL_GetKeyForJoyEvent( localPlayerNum, &joyevent ); if ( key != -1 ) Com_Printf ("\"%s\" = \"%s\"\n", CL_JoyEventToString( &joyevent ), Key_KeynumToString( key ) ); else Com_Printf ("\"%s\" is not remapped\n", CL_JoyEventToString( &joyevent ) ); return; } key = Key_StringToKeynum (Cmd_Argv(2)); if (key==-1) { Com_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(2)); return; } if ( !CL_SetKeyForJoyEvent( localPlayerNum, &joyevent, key ) ) { Com_Printf ("Max joystick remaps reached (%d), cannot add remap for %s.\n", MAX_JOY_REMAPS, CL_JoyEventToString( &joyevent ) ); } }
void Cvar_Set_Bind_Str_f (void) { cvar_t *var; int keynum; char *var_name; char *key_name; //char str[1024]; //char *v,*s; if (Cmd_Argc() != 3) { Com_Printf ("usage: set_bind_str <cvar> <key>\n"); return; } var_name = Cmd_Argv (1); key_name = Cmd_Argv (2); var = Cvar_Find (var_name); keynum = Key_StringToKeynum( key_name ); if ( !var) { if (Cmd_Exists(var_name)) { Com_Printf ("\"%s\" is a command\n", var_name); return; } var = Cvar_Create(var_name, "", 0); } if (!var) { Com_Printf ("Unknown variable \"%s\"\n", var_name); return; } else if (keynum == -1) { Com_Printf ("Unknown key \"%s\"\n", key_name); return; } else { if (keybindings[keynum]) { /* s = str; v = keybindings[keynum]; while (*v) { if (*v == '\"') // " should be escaped *s++ = '\\'; *s++ = *v++; } *s = '\0'; Cvar_Set (var, str);*/ Cvar_Set (var, keybindings[keynum]); } else Cvar_Set (var, ""); } }
void QCBUILTIN PF_cl_stringtokeynum(pubprogfuncs_t *prinst, struct globalvars_s *pr_globals) { int i; int modifier; const char *s; s = PR_GetStringOfs(prinst, OFS_PARM0); i = Key_StringToKeynum(s, &modifier); if (i < 0 || modifier != ~0) { G_FLOAT(OFS_RETURN) = -1; return; } i = MP_TranslateFTEtoQCCodes(i); G_FLOAT(OFS_RETURN) = i; }
/* =================== Key_Unbind_f =================== */ static void Key_Unbind_f(void) { int b; if (Cmd_Argc() != 2) { Com_Printf("unbind <key> : remove commands from a key\n"); return; } b = Key_StringToKeynum(Cmd_Argv(1)); if (b == -1) { Com_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(1)); return; } Key_SetBinding(b, NULL); }
/* =================== Key_Unbind_f =================== */ void Key_Unbind_f( void ) { int b; if( Cmd_Argc() != 2 ) { Msg( "Usage: unbind <key> : remove commands from a key\n" ); return; } b = Key_StringToKeynum( Cmd_Argv( 1 )); if( b == -1 ) { Msg( "\"%s\" isn't a valid key\n", Cmd_Argv( 1 )); return; } Key_SetBinding( b, "" ); }
/** * @brief Binds a key to a given script command * @sa Key_SetBinding */ static void Key_Bind_f (void) { int i, c, b; char cmd[1024]; c = Cmd_Argc(); if (c < 2) { Com_Printf("Usage: %s <key> [command] : attach a command to a key\n", Cmd_Argv(0)); return; } b = Key_StringToKeynum(Cmd_Argv(1)); if (b == -1) { Com_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(1)); return; } if (c == 2) { if (keyBindings[b]) Com_Printf("\"%s\" = \"%s\"\n", Cmd_Argv(1), keyBindings[b]); else Com_Printf("\"%s\" is not bound\n", Cmd_Argv(1)); return; } /* copy the rest of the command line */ cmd[0] = '\0'; /* start out with a null string */ for (i = 2; i < c; i++) { Q_strcat(cmd, Cmd_Argv(i), sizeof(cmd)); if (i != (c - 1)) Q_strcat(cmd, " ", sizeof(cmd)); } if (Q_streq(Cmd_Argv(0), "bindui")) UI_SetKeyBinding(cmd, b); else if (Q_streq(Cmd_Argv(0), "bindmenu")) Key_SetBinding(b, cmd, KEYSPACE_UI); else if (Q_streq(Cmd_Argv(0), "bindbattle")) Key_SetBinding(b, cmd, KEYSPACE_BATTLE); else Key_SetBinding(b, cmd, KEYSPACE_GAME); }
/** * \brief Console callback method to bind key mapping */ PRIVATE void Key_Bind_f( void ) { int i, c, b; char cmd[ 1024 ]; c = Cmd_Argc(); if( c < 2 ) { Com_Printf( "bind <key> [command] : attach a command to a key\n" ); return; } b = Key_StringToKeynum( Cmd_Argv( 1 ) ); if( b == -1 ) { Com_Printf( "\"%s\" isn't a valid key\n", Cmd_Argv( 1 ) ); return; } if( c == 2 ) { if( keybindings[ b ] ) Com_Printf( "\"%s\" = \"%s\"\n", Cmd_Argv( 1 ), keybindings[ b ] ); else Com_Printf( "\"%s\" is not bound\n", Cmd_Argv( 1 ) ); return; } // copy the rest of the command line cmd[ 0 ] = '\0'; // start out with a null string for( i = 2; i < c; ++i ) { com_strlcat( cmd, Cmd_Argv( i ), sizeof( cmd ) ); if( i != (c-1) ) { com_strlcat( cmd, " ", sizeof( cmd ) ); } } Key_SetBinding( b, cmd ); }
/* =================== Key_Bind_f =================== */ void Key_Bind_f( void ) { int i, c, b; char cmd[1024]; c = Cmd_Argc(); if( c < 2 ) { Msg( "Usage: bind <key> [command] : attach a command to a key\n" ); return; } b = Key_StringToKeynum( Cmd_Argv( 1 )); if( b == -1 ) { Msg( "\"%s\" isn't a valid key\n", Cmd_Argv( 1 )); return; } if( c == 2 ) { if( keys[b].binding ) Msg( "\"%s\" = \"%s\"\n", Cmd_Argv( 1 ), keys[b].binding ); else Msg( "\"%s\" is not bound\n", Cmd_Argv( 1 )); return; } // copy the rest of the command line cmd[0] = 0; // start out with a null string for( i = 2; i < c; i++ ) { Q_strcat( cmd, Cmd_Argv( i )); if( i != ( c - 1 )) Q_strcat( cmd, " " ); } Key_SetBinding( b, cmd ); }
/* =================== Key_Bind_f =================== */ void Key_Bind_f( void ) { int i, c, b; char cmd[MAX_STRING_TOKENS]; c = Cmd_Argc(); if ( c < 2 ) { Com_Printf( "bind <key> [command] : attach a command to a key\n" ); return; } b = Key_StringToKeynum( Cmd_Argv( 1 ) ); if ( b == -1 ) { Com_Printf( "\"%s\" isn't a valid key\n", Cmd_Argv( 1 ) ); return; } if ( c == 2 ) { if ( keys[b].binding && keys[b].binding[0] ) Com_Printf( "\"%s\" = \"%s\"\n", Cmd_Argv( 1 ), keys[b].binding ); else Com_Printf( "\"%s\" is not bound\n", Cmd_Argv( 1 ) ); return; } // copy the rest of the command line cmd[0] = '\0'; // start out with a null string for ( i = 2 ; i < c ; i++ ) { strcat( cmd, Cmd_Argv( i ) ); if ( i != ( c-1 ) ) strcat( cmd, " " ); } Key_SetBinding( b, cmd ); }
/* =================== Key_Bind_f =================== */ static void Key_Bind_f (void) { int i, c, b; char cmd[1024]; c = Cmd_Argc(); if (c != 2 && c != 3) { Con_Printf ("bind <key> [command] : attach a command to a key\n"); return; } b = Key_StringToKeynum (Cmd_Argv(1)); if (b == -1) { Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1)); return; } if (c == 2) { if (keybindings[b]) Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] ); else Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) ); return; } // copy the rest of the command line cmd[0] = 0; for (i = 2; i < c; i++) { q_strlcat (cmd, Cmd_Argv(i), sizeof(cmd)); if (i != (c-1)) q_strlcat (cmd, " ", sizeof(cmd)); } Key_SetBinding (b, cmd); }
/** * @brief Unbind a given key binding * @sa Key_SetBinding */ static void Key_Unbind_f (void) { int b; if (Cmd_Argc() != 2) { Com_Printf("Usage: %s <key> : remove commands from a key\n", Cmd_Argv(0)); return; } b = Key_StringToKeynum(Cmd_Argv(1)); if (b == -1) { Com_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(1)); return; } if (Q_streq(Cmd_Argv(0), "unbindmenu")) Key_SetBinding(b, "", KEYSPACE_UI); else if (Q_streq(Cmd_Argv(0), "unbindbattle")) Key_SetBinding(b, "", KEYSPACE_BATTLE); else Key_SetBinding(b, "", KEYSPACE_GAME); }
/* =============== IN_IsConsoleKey TODO: If the SDL_Scancode situation improves, use it instead of both of these methods =============== */ static bool IN_IsConsoleKey( keyNum_t key, const unsigned char character ) { struct consoleKey_t { enum { KEY, CHARACTER } type; union { keyNum_t key; int character; } u; }; static const struct { char name[8]; int key; } modMap[] = { { "shift", K_SHIFT }, { "ctrl", K_CTRL }, { "alt", K_ALT }, { "super", K_SUPER }, }; static consoleKey_t consoleKeys[ MAX_CONSOLE_KEYS ]; static int numConsoleKeys = 0; static int ifMod, unlessMod = 0; // Only parse the variable when it changes if ( cl_consoleKeys->modified ) { const char *text_p, *token; cl_consoleKeys->modified = false; text_p = cl_consoleKeys->string; numConsoleKeys = 0; ifMod = unlessMod = 0; while ( numConsoleKeys < MAX_CONSOLE_KEYS ) { consoleKey_t *c = &consoleKeys[ numConsoleKeys ]; int charCode = 0; token = COM_Parse( &text_p ); if ( !token[ 0 ] ) { break; } if ( token[ 0 ] == '+' && token[ 1 ] ) { for (unsigned i = 0; i < ARRAY_LEN( modMap ); ++i ) { if ( !Q_stricmp( token + 1, modMap[i].name ) ) { ifMod |= 1 << i; } } } else if ( token[ 0 ] == '-' && token[ 1 ] ) { for (unsigned i = 0; i < ARRAY_LEN( modMap ); ++i ) { if ( !Q_stricmp( token + 1, modMap[i].name ) ) { unlessMod |= 1 << i; } } } else if ( strlen( token ) == 4 ) { charCode = Com_HexStrToInt( token ); } if ( charCode > 0 ) { c->type = consoleKey_t::CHARACTER; c->u.character = charCode; } else { c->type = consoleKey_t::KEY; c->u.key = (keyNum_t) Key_StringToKeynum( token ); // 0 isn't a key if ( c->u.key <= 0 ) { continue; } } numConsoleKeys++; } // if MOD is requested pressed and released, clear released unlessMod &= ~ifMod; } // require a +MOD, if there are any, to be pressed if ( ifMod ) { bool flag = false; for (unsigned i = 0; i < ARRAY_LEN( modMap ); ++i ) { if ( ( ifMod & 1 << i ) && keys[ modMap[i].key ].down ) { flag = true; break; } } if ( !flag ) { return false; } } // require all -MOD not to be pressed if ( unlessMod ) { for (unsigned i = 0; i < ARRAY_LEN( modMap ); ++i ) { if ( ( unlessMod & 1 << i ) && keys[ modMap[i].key ].down ) { return false; } } } // If the character is the same as the key, prefer the character if ( key == character ) { key = (keyNum_t) 0; } for (int i = 0; i < numConsoleKeys; i++ ) { consoleKey_t *c = &consoleKeys[ i ]; switch ( c->type ) { case consoleKey_t::KEY: if ( key && c->u.key == key ) { return true; } break; case consoleKey_t::CHARACTER: if ( c->u.character == character ) { return true; } break; } } return false; }
/* ================= CL_OpenJoystickRemap joystickIdent could be a name or hash ================= */ qboolean CL_OpenJoystickRemap( int localPlayerNum, const char *joystickName, const char *joystickIdent ) { fileHandle_t f; char filename[MAX_QPATH]; char *buffer, *text, *token; int len, i; joyevent_t joyevent; int key; if ( !joystickName ) { return qfalse; } if ( !joystickIdent ) { joystickIdent = joystickName; } // check if already loaded for ( i = 0; i < CL_MAX_SPLITVIEW; i++ ) { if ( !strcmp(joyDevice[i].ident, joystickIdent ) ) { break; } } if ( i != CL_MAX_SPLITVIEW ) { playerJoyRemapIndex[localPlayerNum] = i; joyDevice[i].references++; return qtrue; } // find free slot for ( i = 0; i < CL_MAX_SPLITVIEW; i++ ) { if ( !joyDevice[i].references ) { break; } } if ( i == CL_MAX_SPLITVIEW ) { Com_Printf("BUG: Tried to open joystick but no free slot\n"); playerJoyRemapIndex[localPlayerNum] = -1; return qfalse; } playerJoyRemapIndex[localPlayerNum] = i; // initialize remap Com_Memset( &joyDevice[i], 0, sizeof ( joyDevice[0] ) ); Q_strncpyz( joyDevice[i].ident, joystickIdent, sizeof ( joyDevice[i].ident ) ); Q_strncpyz( joyDevice[i].name, joystickName, sizeof ( joyDevice[i].ident ) ); joyDevice[i].references = 1; Com_sprintf( filename, sizeof ( filename ), "joy-%s-%s.txt", JOY_PLATFORM, joyDevice[i].ident ); len = FS_SV_FOpenFileRead( filename, &f ); if ( !f ) { return qfalse; } buffer = Hunk_AllocateTempMemory(len+1); FS_Read (buffer, len, f); // guarantee that it will have a trailing 0 for string operations buffer[len] = 0; FS_FCloseFile( f ); text = buffer; while ( 1 ) { token = COM_Parse( &text ); if ( !*token ) { break; } if ( !CL_StringToJoyEvent( token, &joyevent) ) { SkipRestOfLine( &text ); Com_Printf ("\"%s\" isn't a valid joystick event in %s\n", token, filename ); continue; } token = COM_ParseExt( &text, qfalse ); if ( !*token ) { Com_Printf("WARNING: Missing key for joy event in %s\n", filename ); continue; } key = Key_StringToKeynum( token ); if ( key == -1 ) { Com_Printf( "\"%s\" isn't a valid key in %s\n", token, filename ); continue; } if ( !CL_SetKeyForJoyEvent( localPlayerNum, &joyevent, key ) ) { Com_Printf ("Max joystick remaps reached (%d), cannot add remap for %s.\n", MAX_JOY_REMAPS, CL_JoyEventToString( &joyevent ) ); break; } } Hunk_FreeTempMemory( buffer ); return qtrue; }
static qboolean IN_IsConsoleKey(keyNum_t key, int character) { typedef struct consoleKey_s { enum { KEY, CHARACTER } type; union { keyNum_t key; int character; } u; } consoleKey_t; static consoleKey_t consoleKeys[MAX_CONSOLE_KEYS]; static int numConsoleKeys = 0; int i; // Only parse the variable when it changes if (cl_consoleKeys->modified) { char *text_p, *token; cl_consoleKeys->modified = qfalse; text_p = cl_consoleKeys->string; numConsoleKeys = 0; while (numConsoleKeys < MAX_CONSOLE_KEYS) { consoleKey_t *c = &consoleKeys[numConsoleKeys]; int charCode = 0; token = COM_Parse(&text_p); if (!token[0]) { break; } if (strlen(token) == 4) { charCode = Com_HexStrToInt(token); } if (charCode > 0) { c->type = CHARACTER; c->u.character = charCode; } else { c->type = KEY; c->u.key = Key_StringToKeynum(token); // 0 isn't a key if (c->u.key <= 0) { continue; } } numConsoleKeys++; } } // If the character is the same as the key, prefer the character if (key == character) { key = 0; } for (i = 0; i < numConsoleKeys; i++) { consoleKey_t *c = &consoleKeys[i]; switch (c->type) { case KEY: if (key && c->u.key == key) { return qtrue; } break; case CHARACTER: if (c->u.character == character) { return qtrue; } break; } } return qfalse; }
/* =============== IN_IsConsoleKey TODO: If the SDL_Scancode situation improves, use it instead of both of these methods =============== */ static qboolean IN_IsConsoleKey( fakeAscii_t key, int character ) { typedef struct consoleKey_s { enum { QUAKE_KEY, CHARACTER } type; union { fakeAscii_t key; int character; } u; } consoleKey_t; static consoleKey_t consoleKeys[ MAX_CONSOLE_KEYS ]; static int numConsoleKeys = 0; int i; // Only parse the variable when it changes if( cl_consoleKeys->modified ) { const char *text_p; char *token; cl_consoleKeys->modified = qfalse; text_p = cl_consoleKeys->string; numConsoleKeys = 0; COM_BeginParseSession(); while( numConsoleKeys < MAX_CONSOLE_KEYS ) { consoleKey_t *c = &consoleKeys[ numConsoleKeys ]; int charCode = 0; token = COM_Parse( &text_p ); if( !token[ 0 ] ) break; if( strlen( token ) == 4 ) charCode = Com_HexStrToInt( token ); if( charCode > 0 ) { c->type = consoleKey_t::CHARACTER; c->u.character = charCode; } else { c->type = consoleKey_t::QUAKE_KEY; c->u.key = (fakeAscii_t)Key_StringToKeynum( token ); // 0 isn't a key if( c->u.key <= 0 ) continue; } numConsoleKeys++; } COM_EndParseSession(); } // If the character is the same as the key, prefer the character if( key == character ) key = A_NULL; for( i = 0; i < numConsoleKeys; i++ ) { consoleKey_t *c = &consoleKeys[ i ]; switch( c->type ) { case consoleKey_t::QUAKE_KEY: if( key && c->u.key == key ) return qtrue; break; case consoleKey_t::CHARACTER: if( c->u.character == character ) return qtrue; break; } } return qfalse; }
static qbool IN_IsConsoleKey(keyNum_t key, const unsigned char character) { typedef struct consoleKey_s { enum { KEY, CHARACTER } type; union { keyNum_t key; unsigned char character; } u; } consoleKey_t; static consoleKey_t consoleKeys[ MAX_CONSOLE_KEYS ]; static int numConsoleKeys = 0; int i; /* Only parse the variable when it changes */ if(cl_consoleKeys->modified){ char *text_p, *token; cl_consoleKeys->modified = qfalse; text_p = cl_consoleKeys->string; numConsoleKeys = 0; while(numConsoleKeys < MAX_CONSOLE_KEYS){ consoleKey_t *c = &consoleKeys[ numConsoleKeys ]; int charCode = 0; token = Q_readtok(&text_p); if(!token[ 0 ]) break; if(strlen(token) == 4) charCode = Q_hexstr2int(token); if(charCode > 0){ c->type = CHARACTER; c->u.character = (unsigned char)charCode; }else{ c->type = KEY; c->u.key = Key_StringToKeynum(token); /* 0 isn't a key */ if(c->u.key <= 0) continue; } numConsoleKeys++; } } /* If the character is the same as the key, prefer the character */ if(key == character) key = 0; for(i = 0; i < numConsoleKeys; i++){ consoleKey_t *c = &consoleKeys[ i ]; switch(c->type){ case KEY: if(key && c->u.key == key) return qtrue; break; case CHARACTER: if(c->u.character == character) return qtrue; break; } } return qfalse; }