hedgewars/uKeys.pas
changeset 6909 2b9c1228d516
parent 6906 6f5e1e64f106
child 6917 4889c2b779b4
equal deleted inserted replaced
6908:896ed2afcfb8 6909:2b9c1228d516
    73 // move cursor/camera
    73 // move cursor/camera
    74 // TODO: Scale on screen dimensions and/or axis value (game controller)?
    74 // TODO: Scale on screen dimensions and/or axis value (game controller)?
    75 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
    75 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
    76 
    76 
    77 pkbd:= SDL_GetKeyState(@j);
    77 pkbd:= SDL_GetKeyState(@j);
    78 for i:= 1 to pred(j) do 
    78 for i:= 6 to pred(j) do // first 6 will be overwritten
    79     tkbdn[i]:= pkbd^[i];
    79     tkbdn[i]:= pkbd^[i];
    80 
    80 
    81 k:= SDL_GetMouseState(nil, nil);
    81 k:= SDL_GetMouseState(nil, nil);
    82 // mouse buttons
    82 // mouse buttons
    83 {$IFDEF DARWIN}
    83 {$IFDEF DARWIN}
    84 tkbdn[SDL_SCANCODE_MOUSEL]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
    84 tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
    85 tkbdn[SDL_SCANCODE_MOUSER]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
    85 tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
    86 {$ELSE}
    86 {$ELSE}
    87 tkbdn[SDL_SCANCODE_MOUSEL]:= (k and 1);
    87 tkbdn[1]:= (k and 1);
    88 tkbdn[SDL_SCANCODE_MOUSER]:= ((k shr 2) and 1);
    88 tkbdn[3]:= ((k shr 2) and 1);
    89 {$ENDIF}
    89 {$ENDIF}
    90 tkbdn[SDL_SCANCODE_MOUSEM]:= ((k shr 1) and 1);
    90 tkbdn[2]:= ((k shr 1) and 1);
    91 
    91 
    92 // mouse wheels
    92 // mouse wheels
    93 tkbdn[SDL_SCANCODE_WHEELDOWN]:= ord(wheelDown);
    93 tkbdn[4]:= ord(wheelDown);
    94 tkbdn[SDL_SCANCODE_WHEELUP]:= ord(wheelUp);
    94 tkbdn[5]:= ord(wheelUp);
    95 wheelUp:= false;
    95 wheelUp:= false;
    96 wheelDown:= false;
    96 wheelDown:= false;
    97 
    97 
    98 {$IFDEF MOBILE}
    98 {$IFDEF MOBILE}
    99 setTouchWidgetStates();
    99 setTouchWidgetStates();
   100 {$ENDIF}
   100 {$ENDIF}
   101 
   101 
   102 {$IFNDEF MOBILE}
   102 {$IFNDEF MOBILE}
   103 // Controller(s)
   103 // Controller(s)
   104 k:= SDL_SCANCODE_CONTROLLER; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   104 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   105 for j:= 0 to Pred(ControllerNumControllers) do
   105 for j:= 0 to Pred(ControllerNumControllers) do
   106     begin
   106     begin
   107     for i:= 0 to Pred(ControllerNumAxes[j]) do
   107     for i:= 0 to Pred(ControllerNumAxes[j]) do
   108         begin
   108         begin
   109         if ControllerAxes[j][i] > 20000 then
   109         if ControllerAxes[j][i] > 20000 then
   132     end;
   132     end;
   133 {$ENDIF}
   133 {$ENDIF}
   134 
   134 
   135 // ctrl/cmd + q to close engine and frontend
   135 // ctrl/cmd + q to close engine and frontend
   136 {$IFDEF DARWIN}
   136 {$IFDEF DARWIN}
   137     if ((tkbdn[SDL_SCANCODE_LGUI] = 1) or (tkbdn[SDL_SCANCODE_RGUI] = 1)) then
   137     if ((tkbdn[KeyNameToCode('left_meta')] = 1) or (tkbdn[KeyNameToCode('right_meta')] = 1)) then
   138 {$ELSE}
   138 {$ELSE}
   139     if ((tkbdn[SDL_SCANCODE_LCTRL] = 1) or (tkbdn[SDL_SCANCODE_RCTRL] = 1)) then
   139     if ((tkbdn[KeyNameToCode('left_ctrl')] = 1) or (tkbdn[KeyNameToCode('right_ctrl')] = 1)) then
   140 {$ENDIF}
   140 {$ENDIF}
   141     begin
   141     begin
   142         if tkbdn[SDL_SCANCODE_Q] = 1 then ParseCommand ('halt', true)
   142         if tkbdn[KeyNameToCode('q')] = 1 then ParseCommand ('halt', true)
   143     end;
   143     end;
   144 
   144 
   145 // now process strokes
   145 // now process strokes
   146 for i:= 0 to cKeyMaxIndex do
   146 for i:= 0 to cKeyMaxIndex do
   147 if CurrentBinds[i][0] <> #0 then
   147 if CurrentBinds[i][0] <> #0 then
   179 for i:= 1 to pred(j) do
   179 for i:= 1 to pred(j) do
   180     tkbdn[i]:= pkbd^[i];
   180     tkbdn[i]:= pkbd^[i];
   181 
   181 
   182 // mouse buttons
   182 // mouse buttons
   183 {$IFDEF DARWIN}
   183 {$IFDEF DARWIN}
   184 tkbdn[SDL_SCANCODE_MOUSEL]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
   184 tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
   185 tkbdn[SDL_SCANCODE_MOUSER]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
   185 tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
   186 {$ELSE}
   186 {$ELSE}
   187 tkbdn[SDL_SCANCODE_MOUSEL]:= (k and 1);
   187 tkbdn[1]:= (k and 1);
   188 tkbdn[SDL_SCANCODE_MOUSER]:= ((k shr 2) and 1);
   188 tkbdn[3]:= ((k shr 2) and 1);
   189 {$ENDIF}
   189 {$ENDIF}
   190 tkbdn[SDL_SCANCODE_MOUSEM]:= ((k shr 1) and 1);
   190 tkbdn[2]:= ((k shr 1) and 1);
   191 
   191 
   192 // mouse wheels
   192 // mouse wheels
   193 tkbdn[SDL_SCANCODE_WHEELDOWN]:= ord(wheelDown);
   193 tkbdn[4]:= ord(wheelDown);
   194 tkbdn[SDL_SCANCODE_WHEELUP]:= ord(wheelUp);
   194 tkbdn[5]:= ord(wheelUp);
   195 wheelUp:= false;
   195 wheelUp:= false;
   196 wheelDown:= false;
   196 wheelDown:= false;
   197 
   197 
   198 {$IFDEF MOBILE}
   198 {$IFDEF MOBILE}
   199 setTouchWidgetStates();
   199 setTouchWidgetStates();
   200 {$ENDIF}
   200 {$ENDIF}
   201 
   201 
   202 {$IFNDEF MOBILE}
   202 {$IFNDEF MOBILE}
   203 // Controller(s)
   203 // Controller(s)
   204 k:= SDL_SCANCODE_CONTROLLER; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   204 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   205 for j:= 0 to Pred(ControllerNumControllers) do
   205 for j:= 0 to Pred(ControllerNumControllers) do
   206     begin
   206     begin
   207     for i:= 0 to Pred(ControllerNumAxes[j]) do
   207     for i:= 0 to Pred(ControllerNumAxes[j]) do
   208         begin
   208         begin
   209         if ControllerAxes[j][i] > 20000 then
   209         if ControllerAxes[j][i] > 20000 then
   239 
   239 
   240 procedure InitKbdKeyTable;
   240 procedure InitKbdKeyTable;
   241 var i, j, k, t: LongInt;
   241 var i, j, k, t: LongInt;
   242     s: string[15];
   242     s: string[15];
   243 begin
   243 begin
   244 for i:= 0 to cKeyMaxIndex do
   244 KeyNames[1]:= 'mousel';
       
   245 KeyNames[2]:= 'mousem';
       
   246 KeyNames[3]:= 'mouser';
       
   247 KeyNames[4]:= 'wheelup';
       
   248 KeyNames[5]:= 'wheeldown';
       
   249 
       
   250 for i:= 6 to cKeyMaxIndex do
   245     begin
   251     begin
   246     s:= shortstring(sdl_getkeyname(i));
   252     s:= shortstring(sdl_getkeyname(i));
   247     //WriteToConsole(IntToStr(i) + ': ' + s);
   253     //WriteToConsole(IntToStr(i) + ': ' + s);
   248     if s = 'unknown key' then KeyNames[i]:= ''
   254     if s = 'unknown key' then KeyNames[i]:= ''
   249     else 
   255     else 
   253                 s[t]:= '_';
   259                 s[t]:= '_';
   254         KeyNames[i]:= s
   260         KeyNames[i]:= s
   255         end;
   261         end;
   256     end;
   262     end;
   257 
   263 
   258 KeyNames[SDL_SCANCODE_MOUSEL]:= 'mousel';
       
   259 KeyNames[SDL_SCANCODE_MOUSEM]:= 'mousem';
       
   260 KeyNames[SDL_SCANCODE_MOUSER]:= 'mouser';
       
   261 KeyNames[SDL_SCANCODE_WHEELUP]:= 'wheelup';
       
   262 KeyNames[SDL_SCANCODE_WHEELDOWN]:= 'wheeldown';
       
   263 //for i:= 0 to cKeyMaxIndex do writeln(stdout,IntToStr(i) + ': ' + KeyNames[i]);
   264 //for i:= 0 to cKeyMaxIndex do writeln(stdout,IntToStr(i) + ': ' + KeyNames[i]);
   264 
   265 
   265 k:= SDL_SCANCODE_CONTROLLER;
   266 // get the size of keyboard array
       
   267 SDL_GetKeyState(@k);
       
   268 
   266 // Controller(s)
   269 // Controller(s)
   267 for j:= 0 to Pred(ControllerNumControllers) do
   270 for j:= 0 to Pred(ControllerNumControllers) do
   268     begin
   271     begin
   269     for i:= 0 to Pred(ControllerNumAxes[j]) do
   272     for i:= 0 to Pred(ControllerNumAxes[j]) do
   270         begin
   273         begin
   285         keynames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
   288         keynames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
   286         inc(k, 1);
   289         inc(k, 1);
   287         end;
   290         end;
   288     end;
   291     end;
   289 
   292 
   290 DefaultBinds[SDL_SCANCODE_ESCAPE]:= 'quit';
   293 DefaultBinds[ 27]:= 'quit';
   291 DefaultBinds[SDL_SCANCODE_GRAVE]:= 'history';
   294 DefaultBinds[ 96]:= 'history';
   292 DefaultBinds[127]:= 'rotmask';
   295 DefaultBinds[127]:= 'rotmask';
   293 
   296 
   294 //numpad
   297 //numpad
   295 //DefaultBinds[265]:= '+volup';
   298 //DefaultBinds[265]:= '+volup';
   296 //DefaultBinds[256]:= '+voldown';
   299 //DefaultBinds[256]:= '+voldown';
   297 
   300 
   298 DefaultBinds[SDL_SCANCODE_0]:= '+volup';
   301 DefaultBinds[KeyNameToCode('0')]:= '+volup';
   299 DefaultBinds[SDL_SCANCODE_9]:= '+voldown';
   302 DefaultBinds[KeyNameToCode('9')]:= '+voldown';
   300 DefaultBinds[SDL_SCANCODE_C]:= 'capture';
   303 DefaultBinds[KeyNameToCode('c')]:= 'capture';
   301 DefaultBinds[SDL_SCANCODE_H]:= 'findhh';
   304 DefaultBinds[KeyNameToCode('h')]:= 'findhh';
   302 DefaultBinds[SDL_SCANCODE_P]:= 'pause';
   305 DefaultBinds[KeyNameToCode('p')]:= 'pause';
   303 DefaultBinds[SDL_SCANCODE_S]:= '+speedup';
   306 DefaultBinds[KeyNameToCode('s')]:= '+speedup';
   304 DefaultBinds[SDL_SCANCODE_T]:= 'chat';
   307 DefaultBinds[KeyNameToCode('t')]:= 'chat';
   305 DefaultBinds[SDL_SCANCODE_Y]:= 'confirm';
   308 DefaultBinds[KeyNameToCode('y')]:= 'confirm';
   306 
   309 
   307 DefaultBinds[SDL_SCANCODE_MOUSEM]:= 'zoomreset';
   310 DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
   308 DefaultBinds[SDL_SCANCODE_WHEELUP]:= 'zoomout';
   311 DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomout';
   309 DefaultBinds[SDL_SCANCODE_WHEELDOWN]:= 'zoomin';
   312 DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomin';
   310 
   313 
   311 DefaultBinds[SDL_SCANCODE_F12]:= 'fullscr';
   314 DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
   312 
   315 
   313 
   316 
   314 DefaultBinds[SDL_SCANCODE_MOUSEL]:= '/put';
   317 DefaultBinds[ 1]:= '/put';
   315 DefaultBinds[SDL_SCANCODE_MOUSER]:= 'ammomenu';
   318 DefaultBinds[ 3]:= 'ammomenu';
   316 DefaultBinds[SDL_SCANCODE_BACKSPACE]:= 'hjump';
   319 DefaultBinds[ 8]:= 'hjump';
   317 DefaultBinds[SDL_SCANCODE_TAB]:= 'switch';
   320 DefaultBinds[ 9]:= 'switch';
   318 DefaultBinds[SDL_SCANCODE_RETURN]:= 'ljump';
   321 DefaultBinds[13]:= 'ljump';
   319 DefaultBinds[SDL_SCANCODE_SPACE]:= '+attack';
   322 DefaultBinds[32]:= '+attack';
   320 
   323 {$IFDEF MOBILE}
   321 DefaultBinds[SDL_SCANCODE_UP]:= '+up';
   324 DefaultBinds[23]:= '+up';
   322 DefaultBinds[SDL_SCANCODE_DOWN]:= '+down';
   325 DefaultBinds[24]:= '+down';
   323 DefaultBinds[SDL_SCANCODE_LEFT]:= '+left';
   326 DefaultBinds[25]:= '+left';
   324 DefaultBinds[SDL_SCANCODE_RIGHT]:= '+right';
   327 DefaultBinds[26]:= '+right';
   325 DefaultBinds[SDL_SCANCODE_LSHIFT]:= '+precise';
   328 DefaultBinds[27]:= '+precise';
   326 
   329 DefaultBinds[44]:= 'chat';
   327 DefaultBinds[SDL_SCANCODE_F1]:= 'slot 1';
   330 DefaultBinds[55]:= 'pause';
   328 DefaultBinds[SDL_SCANCODE_F2]:= 'slot 2';
   331 {$ELSE}
   329 DefaultBinds[SDL_SCANCODE_F3]:= 'slot 3';
   332 DefaultBinds[KeyNameToCode('up')]:= '+up';
   330 DefaultBinds[SDL_SCANCODE_F4]:= 'slot 4';
   333 DefaultBinds[KeyNameToCode('down')]:= '+down';
   331 DefaultBinds[SDL_SCANCODE_F5]:= 'slot 5';
   334 DefaultBinds[KeyNameToCode('left')]:= '+left';
   332 DefaultBinds[SDL_SCANCODE_F6]:= 'slot 6';
   335 DefaultBinds[KeyNameToCode('right')]:= '+right';
   333 DefaultBinds[SDL_SCANCODE_F7]:= 'slot 7';
   336 DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
   334 DefaultBinds[SDL_SCANCODE_F8]:= 'slot 8';
   337 {$ENDIF}
   335 DefaultBinds[SDL_SCANCODE_F9]:= 'slot 9';
   338 
   336 DefaultBinds[SDL_SCANCODE_F10]:= 'slot 10';
   339 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
   337 
   340 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
   338 DefaultBinds[SDL_SCANCODE_1]:= 'timer 1';
       
   339 DefaultBinds[SDL_SCANCODE_2]:= 'timer 2';
       
   340 DefaultBinds[SDL_SCANCODE_3]:= 'timer 3';
       
   341 DefaultBinds[SDL_SCANCODE_4]:= 'timer 4';
       
   342 DefaultBinds[SDL_SCANCODE_5]:= 'timer 5';
       
   343 
   341 
   344 SetDefaultBinds();
   342 SetDefaultBinds();
   345 end;
   343 end;
   346 
   344 
   347 procedure SetBinds(var binds: TBinds);
   345 procedure SetBinds(var binds: TBinds);
   360 end;
   358 end;
   361 
   359 
   362 {$IFDEF MOBILE}
   360 {$IFDEF MOBILE}
   363 procedure setTouchWidgetStates;
   361 procedure setTouchWidgetStates;
   364 begin
   362 begin
   365     tkbdn[SDL_SCANCODE_MOUSEL]:= tkbdn[SDL_SCANCODE_MOUSEL] or ord(leftClick);
   363     tkbdn[ 1]:= ord(leftClick);
   366     tkbdn[SDL_SCANCODE_MOUSEM]:= tkbdn[SDL_SCANCODE_MOUSEM] or ord(middleClick);
   364     tkbdn[ 2]:= ord(middleClick);
   367     tkbdn[SDL_SCANCODE_MOUSER]:= tkbdn[SDL_SCANCODE_MOUSER] or ord(rightClick);
   365     tkbdn[ 3]:= ord(rightClick);
   368 
   366 
   369     tkbdn[SDL_SCANCODE_UP]    := tkbdn[SDL_SCANCODE_UP] or ord(upKey);
   367     tkbdn[23]:= ord(upKey);
   370     tkbdn[SDL_SCANCODE_DOWN]  := tkbdn[SDL_SCANCODE_DOWN] or ord(downKey);
   368     tkbdn[24]:= ord(downKey);
   371     tkbdn[SDL_SCANCODE_LEFT]  := tkbdn[SDL_SCANCODE_LEFT] or ord(leftKey);
   369     tkbdn[25]:= ord(leftKey);
   372     tkbdn[SDL_SCANCODE_RIGHT] := tkbdn[SDL_SCANCODE_RIGHT] or ord(rightKey);
   370     tkbdn[26]:= ord(rightKey);
   373     tkbdn[SDL_SCANCODE_LSHIFT]:= tkbdn[SDL_SCANCODE_LSHIFT] or ord(preciseKey);
   371     tkbdn[27]:= ord(preciseKey);
   374 
   372 
   375     tkbdn[SDL_SCANCODE_BACKSPACE]:= tkbdn[SDL_SCANCODE_BACKSPACE] or ord(backspaceKey);
   373     tkbdn[ 8]:= ord(backspaceKey);
   376     tkbdn[SDL_SCANCODE_TAB]:= tkbdn[SDL_SCANCODE_TAB] or ord(tabKey);
   374     tkbdn[ 9]:= ord(tabKey);
   377     tkbdn[SDL_SCANCODE_RETURN]:= ord(enterKey);
   375     tkbdn[13]:= ord(enterKey);
   378     tkbdn[SDL_SCANCODE_SPACE]:= ord(spaceKey);
   376     tkbdn[32]:= ord(spaceKey);
   379 
   377 
   380     tkbdn[SDL_SCANCODE_T]:= tkbdn[SDL_SCANCODE_T] or ord(chatAction);
   378     tkbdn[44]:= ord(chatAction);
   381     tkbdn[SDL_SCANCODE_PAUSE]:= ord(pauseAction);
   379     tkbdn[55]:= ord(pauseAction);
   382 
   380 
   383     // set to false the keys that only need one stoke
   381     // set to false the keys that only need one stoke
   384     leftClick:= false;
   382     leftClick:= false;
   385     middleClick:= false;
   383     middleClick:= false;
   386     rightClick:= false;
   384     rightClick:= false;