aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/client/Key.java
blob: 2fe8ed35f6fe4649db0aa6eff86b95a8add4b701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
 * Key.java
 * Copyright (C) 2003
 * 
 * $Id: Key.java,v 1.2 2004-07-08 15:58:43 hzi Exp $
 */
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
package jake2.client;

import java.io.IOException;
import java.io.RandomAccessFile;

import jake2.Defines;
import jake2.Globals;
import jake2.game.Cmd;
import jake2.qcommon.*;
import jake2.qcommon.Cbuf;
import jake2.qcommon.Com;
import jake2.util.Lib;

/**
 * Key
 */
public class Key extends Globals {
	//
	// these are the key numbers that should be passed to Key_Event
	//
	public static final int K_TAB = 9;
	public static final int K_ENTER = 13;
	public static final int K_ESCAPE = 27;
	public static final int K_SPACE = 32;

	// normal keys should be passed as lowercased ascii

	public static final int K_BACKSPACE = 127;
	public static final int K_UPARROW = 128;
	public static final int K_DOWNARROW = 129;
	public static final int K_LEFTARROW = 130;
	public static final int K_RIGHTARROW = 131;

	public static final int K_ALT = 132;
	public static final int K_CTRL = 133;
	public static final int K_SHIFT = 134;
	public static final int K_F1 = 135;
	public static final int K_F2 = 136;
	public static final int K_F3 = 137;
	public static final int K_F4 = 138;
	public static final int K_F5 = 139;
	public static final int K_F6 = 140;
	public static final int K_F7 = 141;
	public static final int K_F8 = 142;
	public static final int K_F9 = 143;
	public static final int K_F10 = 144;
	public static final int K_F11 = 145;
	public static final int K_F12 = 146;
	public static final int K_INS = 147;
	public static final int K_DEL = 148;
	public static final int K_PGDN = 149;
	public static final int K_PGUP = 150;
	public static final int K_HOME = 151;
	public static final int K_END = 152;

	public static final int K_KP_HOME = 160;
	public static final int K_KP_UPARROW = 161;
	public static final int K_KP_PGUP = 162;
	public static final int K_KP_LEFTARROW = 163;
	public static final int K_KP_5 = 164;
	public static final int K_KP_RIGHTARROW = 165;
	public static final int K_KP_END = 166;
	public static final int K_KP_DOWNARROW = 167;
	public static final int K_KP_PGDN = 168;
	public static final int K_KP_ENTER = 169;
	public static final int K_KP_INS = 170;
	public static final int K_KP_DEL = 171;
	public static final int K_KP_SLASH = 172;
	public static final int K_KP_MINUS = 173;
	public static final int K_KP_PLUS = 174;

	public static final int K_PAUSE = 255;

	//
	// mouse buttons generate virtual keys
	//
	public static final int K_MOUSE1 = 200;
	public static final int K_MOUSE2 = 201;
	public static final int K_MOUSE3 = 202;

	//
	// joystick buttons
	//
	public static final int K_JOY1 = 203;
	public static final int K_JOY2 = 204;
	public static final int K_JOY3 = 205;
	public static final int K_JOY4 = 206;

	public static final int K_MWHEELDOWN = 239;
	public static final int K_MWHEELUP = 240;

	static int anykeydown = 0;
	static int key_waiting;
	static int history_line = 0;
	static boolean shift_down = false;
	static int[] key_repeats = new int[256];
	static int[] keyshift = new int[256];
	static boolean[] menubound = new boolean[256];
	static boolean[] consolekeys = new boolean[256];

	static String[] keynames = new String[256];

	static {
		keynames[K_TAB] = "TAB";
		keynames[K_ENTER] = "ENTER";
		keynames[K_ESCAPE] = "ESCAPE";
		keynames[K_SPACE] = "SPACE";
		keynames[K_BACKSPACE] = "BACKSPACE";
		keynames[K_UPARROW] = "UPARROW";
		keynames[K_DOWNARROW] = "DOWNARROW";
		keynames[K_LEFTARROW] = "LEFTARROW";
		keynames[K_RIGHTARROW] = "RIGHTARROW";
		keynames[K_ALT] = "ALT";
		keynames[K_CTRL] = "CTRL";
		keynames[K_SHIFT] = "SHIFT";

		keynames[K_F1] = "F1";
		keynames[K_F2] = "F2";
		keynames[K_F3] = "F3";
		keynames[K_F4] = "F4";
		keynames[K_F5] = "F5";
		keynames[K_F6] = "F6";
		keynames[K_F7] = "F7";
		keynames[K_F8] = "F8";
		keynames[K_F9] = "F9";
		keynames[K_F10] = "F10";
		keynames[K_F11] = "F11";
		keynames[K_F12] = "F12";

		keynames[K_INS] = "INS";
		keynames[K_DEL] = "DEL";
		keynames[K_PGDN] = "PGDN";
		keynames[K_PGUP] = "PGUP";
		keynames[K_HOME] = "HOME";
		keynames[K_END] = "END";

		keynames[K_MOUSE1] = "MOUSE1";
		keynames[K_MOUSE2] = "MOUSE2";
		keynames[K_MOUSE3] = "MOUSE3";

		//	00092         {"JOY1", K_JOY1},
		//	00093         {"JOY2", K_JOY2},
		//	00094         {"JOY3", K_JOY3},
		//	00095         {"JOY4", K_JOY4},

		keynames[K_KP_HOME] = "KP_HOME";
		keynames[K_KP_UPARROW] = "KP_UPARROW";
		keynames[K_KP_PGUP] = "KP_PGUP";
		keynames[K_KP_LEFTARROW] = "KP_LEFTARROW";
		keynames[K_KP_5] = "KP_5";
		keynames[K_KP_RIGHTARROW] = "KP_RIGHTARROW";
		keynames[K_KP_END] = "KP_END";
		keynames[K_KP_DOWNARROW] = "KP_DOWNARROW";
		keynames[K_KP_PGDN] = "KP_PGDN";
		keynames[K_KP_ENTER] = "KP_ENTER";
		keynames[K_KP_INS] = "KP_INS";
		keynames[K_KP_DEL] = "KP_DEL";
		keynames[K_KP_SLASH] = "KP_SLASH";

		keynames[K_KP_PLUS] = "KP_PLUS";
		keynames[K_KP_MINUS] = "KP_MINUS";

		keynames[K_MWHEELUP] = "MWHEELUP";
		keynames[K_MWHEELDOWN] = "MWHEELDOWN";

		keynames[K_PAUSE] = "PAUSE";
		keynames[';'] = "SEMICOLON"; // because a raw semicolon seperates commands

		keynames[0] = "NULL";
	}

	/**
	 * 
	 */
	public static void Init() {
		for (int i = 0; i < 32; i++) {
			Globals.key_lines[i][0] = ']';
			Globals.key_lines[i][1] = 0;
		}
		Globals.key_linepos = 1;

		//
		// init ascii characters in console mode
		//
		for (int i = 32; i < 128; i++)
			consolekeys[i] = true;
		consolekeys[K_ENTER] = true;
		consolekeys[K_KP_ENTER] = true;
		consolekeys[K_TAB] = true;
		consolekeys[K_LEFTARROW] = true;
		consolekeys[K_KP_LEFTARROW] = true;
		consolekeys[K_RIGHTARROW] = true;
		consolekeys[K_KP_RIGHTARROW] = true;
		consolekeys[K_UPARROW] = true;
		consolekeys[K_KP_UPARROW] = true;
		consolekeys[K_DOWNARROW] = true;
		consolekeys[K_KP_DOWNARROW] = true;
		consolekeys[K_BACKSPACE] = true;
		consolekeys[K_HOME] = true;
		consolekeys[K_KP_HOME] = true;
		consolekeys[K_END] = true;
		consolekeys[K_KP_END] = true;
		consolekeys[K_PGUP] = true;
		consolekeys[K_KP_PGUP] = true;
		consolekeys[K_PGDN] = true;
		consolekeys[K_KP_PGDN] = true;
		consolekeys[K_SHIFT] = true;
		consolekeys[K_INS] = true;
		consolekeys[K_KP_INS] = true;
		consolekeys[K_KP_DEL] = true;
		consolekeys[K_KP_SLASH] = true;
		consolekeys[K_KP_PLUS] = true;
		consolekeys[K_KP_MINUS] = true;
		consolekeys[K_KP_5] = true;

		consolekeys['`'] = false;
		consolekeys['~'] = false;

		for (int i = 0; i < 256; i++)
			keyshift[i] = i;
		for (int i = 'a'; i <= 'z'; i++)
			keyshift[i] = i - 'a' + 'A';
		keyshift['1'] = '!';
		keyshift['2'] = '@';
		keyshift['3'] = '#';
		keyshift['4'] = '$';
		keyshift['5'] = '%';
		keyshift['6'] = '^';
		keyshift['7'] = '&';
		keyshift['8'] = '*';
		keyshift['9'] = '(';
		keyshift['0'] = ')';
		keyshift['-'] = '_';
		keyshift['='] = '+';
		keyshift[','] = '<';
		keyshift['.'] = '>';
		keyshift['/'] = '?';
		keyshift[';'] = ':';
		keyshift['\''] = '"';
		keyshift['['] = '{';
		keyshift[']'] = '}';
		keyshift['`'] = '~';
		keyshift['\\'] = '|';

		menubound[K_ESCAPE] = true;
		for (int i = 0; i < 12; i++)
			menubound[K_F1 + i] = true;

		//
		// register our functions
		//
		Cmd.AddCommand("bind", Key.Bind_f);
		Cmd.AddCommand("unbind", Key.Unbind_f);
		Cmd.AddCommand("unbindall", Key.Unbindall_f);
		Cmd.AddCommand("bindlist", Key.Bindlist_f);
	}

	public static void ClearTyping() {
		Globals.key_lines[Globals.edit_line][1] = 0; // clear any typing
		Globals.key_linepos = 1;
	}

	/**
	 * Called by the system between frames for both key up and key down events.
	 */
	public static void Event(int key, boolean down, long time) {
		//System.out.println(key +  " " + down);
		//return;
		String kb;
		String cmd;

		// hack for modal presses
		if (key_waiting == -1) {
			if (down)
				key_waiting = key;
			return;
		}

		// update auto-repeat status
		if (down) {
			key_repeats[key]++;
			if (key != K_BACKSPACE
				&& key != K_PAUSE
				&& key != K_PGUP
				&& key != K_KP_PGUP
				&& key != K_PGDN
				&& key != K_KP_PGDN
				&& key_repeats[key] > 1)
				return; // ignore most autorepeats

			if (key >= 200 && Globals.keybindings[key] == null)
				Com.Printf(Key.KeynumToString(key) + " is unbound, hit F4 to set.\n");
		}
		else {
			key_repeats[key] = 0;
		}

		if (key == K_SHIFT)
			shift_down = down;

		// console key is hardcoded, so the user can never unbind it
		if (key == '`' || key == '~') {
			if (!down)
				return;
			try {
				Console.ToggleConsole_f.execute();
			}
			catch (Exception e) {
			}
			return;
		}

		// any key during the attract mode will bring up the menu
		if (Globals.cl.attractloop && Globals.cls.key_dest != Defines.key_menu && !(key >= K_F1 && key <= K_F12))
			key = K_ESCAPE;

		// menu key is hardcoded, so the user can never unbind it
		if (key == K_ESCAPE) {
			if (!down)
				return;

			if (Globals.cl.frame.playerstate.stats[Defines.STAT_LAYOUTS] != 0 && Globals.cls.key_dest == Defines.key_game) {
				// put away help computer / inventory
				Cbuf.AddText("cmd putaway\n");
				return;
			}
			switch (Globals.cls.key_dest) {
				case Defines.key_message :
					Key.Message(key);
					break;
				case Defines.key_menu :
					Menu.Keydown(key);
					break;
				case Defines.key_game :
				case Defines.key_console :
					Menu.Menu_Main_f();
					break;
				default :
					Com.Error(Defines.ERR_FATAL, "Bad cls.key_dest");
			}
			return;
		}

		// track if any key is down for BUTTON_ANY
		Globals.keydown[key] = down;
		if (down) {
			if (key_repeats[key] == 1)
				Key.anykeydown++;
		}
		else {
			Key.anykeydown--;
			if (Key.anykeydown < 0)
				Key.anykeydown = 0;
		}

		//
		// key up events only generate commands if the game key binding is
		// a button command (leading + sign).  These will occur even in console mode,
		// to keep the character from continuing an action started before a console
		// switch.  Button commands include the kenum as a parameter, so multiple
		// downs can be matched with ups
		//
		if (!down) {
			kb = Globals.keybindings[key];
			if (kb != null && kb.length()>0 && kb.charAt(0) == '+') {
				cmd = "-" + kb.substring(1) + " " + key + " " + time + "\n";
				Cbuf.AddText(cmd);
			}
			if (keyshift[key] != key) {
				kb = Globals.keybindings[keyshift[key]];
				if (kb != null && kb.length()>0 && kb.charAt(0) == '+') {
					cmd = "-" + kb.substring(1) + " " + key + " " + time + "\n";
					Cbuf.AddText(cmd);
				}
			}
			return;
		}

		//
		// if not a consolekey, send to the interpreter no matter what mode is
		//
		if ((Globals.cls.key_dest == Defines.key_menu && menubound[key])
			|| (Globals.cls.key_dest == Defines.key_console && !consolekeys[key])
			|| (Globals.cls.key_dest == Defines.key_game && (Globals.cls.state == Defines.ca_active || !consolekeys[key]))) {
			kb = Globals.keybindings[key];
			if (kb != null) {
				if (kb.length()>0 && kb.charAt(0) == '+') {
					// button commands add keynum and time as a parm
					cmd = kb + " " + key + " " + time + "\n";
					Cbuf.AddText(cmd);
				}
				else {
					Cbuf.AddText(kb + "\n");
				}
			}
			return;
		}

		if (!down)
			return; // other systems only care about key down events

		if (shift_down)
			key = keyshift[key];

		switch (Globals.cls.key_dest) {
			case Defines.key_message :
				Key.Message(key);
				break;
			case Defines.key_menu :
				Menu.Keydown(key);
				break;

			case Defines.key_game :
			case Defines.key_console :
				Key.Console(key);
				break;
			default :
				Com.Error(Defines.ERR_FATAL, "Bad cls.key_dest");
		}
	}

	/**
	 * Returns a string (either a single ascii char, or a K_* name) for the 
	 * given keynum.
	 */
	public static String KeynumToString(int keynum) {
		if (keynum < 0 || keynum > 255)
			return "<KEY NOT FOUND>";
		if (keynum > 32 && keynum < 127)
			return Character.toString((char) keynum);

		if (keynames[keynum] != null)
			return keynames[keynum];

		return "<UNKNOWN KEYNUM>";
	}

	/**
	 * Returns a key number to be used to index keybindings[] by looking at
	 * the given string. Single ascii characters return themselves, while
	 * the K_* names are matched up.
	 */
	static int StringToKeynum(String str) {

		if (str == null)
			return -1;

		if (str.length() == 1)
			return str.charAt(0);

		for (int i = 0; i < keynames.length; i++) {
			if (str.equalsIgnoreCase(keynames[i]))
				return i;
		}

		return -1;
	}

	public static void Message(int key) {

		if (key == K_ENTER || key == K_KP_ENTER) {
			if (Globals.chat_team)
				Cbuf.AddText("say_team \"");
			else
				Cbuf.AddText("say \"");

			Cbuf.AddText(Globals.chat_buffer);
			Cbuf.AddText("\"\n");

			Globals.cls.key_dest = Defines.key_game;
			Globals.chat_buffer = "";
			return;
		}

		if (key == K_ESCAPE) {
			Globals.cls.key_dest = Defines.key_game;
			Globals.chat_buffer = "";
			return;
		}

		if (key < 32 || key > 127)
			return; // non printable

		if (key == K_BACKSPACE) {
			if (Globals.chat_buffer.length() > 2) {
				Globals.chat_buffer = Globals.chat_buffer.substring(0, Globals.chat_buffer.length() - 2);
			}
			else
				Globals.chat_buffer = "";
			return;
		}

		if (Globals.chat_buffer.length() > Defines.MAXCMDLINE)
			return; // all full

		Globals.chat_buffer += (char) key;
	}

	/**
	 * Interactive line editing and console scrollback.
	 */
	public static void Console(int key) {

		switch (key) {
			case K_KP_SLASH :
				key = '/';
				break;
			case K_KP_MINUS :
				key = '-';
				break;
			case K_KP_PLUS :
				key = '+';
				break;
			case K_KP_HOME :
				key = '7';
				break;
			case K_KP_UPARROW :
				key = '8';
				break;
			case K_KP_PGUP :
				key = '9';
				break;
			case K_KP_LEFTARROW :
				key = '4';
				break;
			case K_KP_5 :
				key = '5';
				break;
			case K_KP_RIGHTARROW :
				key = '6';
				break;
			case K_KP_END :
				key = '1';
				break;
			case K_KP_DOWNARROW :
				key = '2';
				break;
			case K_KP_PGDN :
				key = '3';
				break;
			case K_KP_INS :
				key = '0';
				break;
			case K_KP_DEL :
				key = '.';
				break;
		}

		if (key == 'l') {
			if (Globals.keydown[K_CTRL]) {
				Cbuf.AddText("clear\n");
				return;
			}
		}

		if (key == K_ENTER || key == K_KP_ENTER) {
			// backslash text are commands, else chat
			if (Globals.key_lines[Globals.edit_line][1] == '\\' || Globals.key_lines[Globals.edit_line][1] == '/')
				Cbuf.AddText(
					new String(Globals.key_lines[Globals.edit_line], 2, Lib.strlen(Globals.key_lines[Globals.edit_line]) - 2));
			else
				Cbuf.AddText(
					new String(Globals.key_lines[Globals.edit_line], 1, Lib.strlen(Globals.key_lines[Globals.edit_line]) - 1));

			
			Cbuf.AddText("\n");
		
			Com.Printf(new String(Globals.key_lines[Globals.edit_line], 0, Lib.strlen(Globals.key_lines[Globals.edit_line])) + "\n");
			Globals.edit_line = (Globals.edit_line + 1) & 31;
			history_line = Globals.edit_line;
		
			Globals.key_lines[Globals.edit_line][0] = ']';
			Globals.key_linepos = 1;
			if (Globals.cls.state == Defines.ca_disconnected)
				SCR.UpdateScreen(); // force an update, because the command may take some time
			return;
		}

		if (key == K_TAB) {
			// command completion
			CompleteCommand();
			return;
		}

		if ((key == K_BACKSPACE) || (key == K_LEFTARROW) || (key == K_KP_LEFTARROW) || ((key == 'h') && (Globals.keydown[K_CTRL]))) {
			if (Globals.key_linepos > 1)
				Globals.key_linepos--;
			return;
		}

		if ((key == K_UPARROW) || (key == K_KP_UPARROW) || ((key == 'p') && Globals.keydown[K_CTRL])) {
			do {
				history_line = (history_line - 1) & 31;
			}
			while (history_line != Globals.edit_line && Globals.key_lines[history_line][1] == 0);
			if (history_line == Globals.edit_line)
				history_line = (Globals.edit_line + 1) & 31;
			Lib.strcpy(Globals.key_lines[Globals.edit_line], Globals.key_lines[history_line]);
			Globals.key_linepos = Lib.strlen(Globals.key_lines[Globals.edit_line]);
			return;
		}

		if ((key == K_DOWNARROW) || (key == K_KP_DOWNARROW) || ((key == 'n') && Globals.keydown[K_CTRL])) {
			if (history_line == Globals.edit_line)
				return;
			do {
				history_line = (history_line + 1) & 31;
			}
			while (history_line != Globals.edit_line && Globals.key_lines[history_line][1] == 0);
			if (history_line == Globals.edit_line) {
				Globals.key_lines[Globals.edit_line][0] = ']';
				Globals.key_linepos = 1;
			}
			else {
				Lib.strcpy(Globals.key_lines[Globals.edit_line], Globals.key_lines[history_line]);
				Globals.key_linepos = Lib.strlen(Globals.key_lines[Globals.edit_line]);
			}
			return;
		}

		if (key == K_PGUP || key == K_KP_PGUP) {
			Globals.con.display -= 2;
			return;
		}

		if (key == K_PGDN || key == K_KP_PGDN) {
			Globals.con.display += 2;
			if (Globals.con.display > Globals.con.current)
				Globals.con.display = Globals.con.current;
			return;
		}

		if (key == K_HOME || key == K_KP_HOME) {
			Globals.con.display = Globals.con.current - Globals.con.totallines + 10;
			return;
		}

		if (key == K_END || key == K_KP_END) {
			Globals.con.display = Globals.con.current;
			return;
		}

		if (key < 32 || key > 127)
			return; // non printable

		if (Globals.key_linepos < Defines.MAXCMDLINE - 1) {
			Globals.key_lines[Globals.edit_line][Globals.key_linepos] = (byte) key;
			Globals.key_linepos++;
			Globals.key_lines[Globals.edit_line][Globals.key_linepos] = 0;
		}

	}

	static void CompleteCommand() {
		//	00166         char    *cmd, *s;
		//	00167 
		//	00168         s = key_lines[edit_line]+1;
		//	00169         if (*s == '\\' || *s == '/')
		//	00170                 s++;
		//	00171 
		//	00172         cmd = Cmd_CompleteCommand (s);
		//	00173         if (!cmd)
		//	00174                 cmd = Cvar_CompleteVariable (s);
		//	00175         if (cmd)
		//	00176         {
		//	00177                 key_lines[edit_line][1] = '/';
		//	00178                 strcpy (key_lines[edit_line]+2, cmd);
		//	00179                 key_linepos = strlen(cmd)+2;
		//	00180                 key_lines[edit_line][key_linepos] = ' ';
		//	00181                 key_linepos++;
		//	00182                 key_lines[edit_line][key_linepos] = 0;
		//	00183                 return;
		//	00184         }
	}

	public static xcommand_t Bind_f = new xcommand_t() {
		public void execute() {
			Key_Bind_f();
		}
	};

	static void Key_Bind_f() {
		int c = Cmd.Argc();

		if (c < 2) {
			Com.Printf("bind <key> [command] : attach a command to a key\n");
			return;
		}
		int b = StringToKeynum(Cmd.Argv(1));
		if (b == -1) {
			Com.Printf("\"" + Cmd.Argv(1) + "\" isn't a valid key\n");
			return;
		}

		if (c == 2) {
			if (Globals.keybindings[b] != null)
				Com.Printf("\"" + Cmd.Argv(1) + "\" = \"" + Globals.keybindings[b] + "\"\n");
			else
				Com.Printf("\"" + Cmd.Argv(1) + "\" is not bound\n");
			return;
		}

		// copy the rest of the command line
		String cmd = ""; // start out with a null string
		for (int i = 2; i < c; i++) {
			cmd += Cmd.Argv(i);
			if (i != (c - 1))
				cmd += " ";
		}

		SetBinding(b, cmd);
	}

	static void SetBinding(int keynum, String binding) {
		if (keynum == -1)
			return;

		// free old bindings
		Globals.keybindings[keynum] = null;

		Globals.keybindings[keynum] = binding;
	}

	static xcommand_t Unbind_f = new xcommand_t() {
		public void execute() {
			Key_Unbind_f();
		}
	};

	static void Key_Unbind_f() {

		if (Cmd.Argc() != 2) {
			Com.Printf("unbind <key> : remove commands from a key\n");
			return;
		}

		int b = Key.StringToKeynum(Cmd.Argv(1));
		if (b == -1) {
			Com.Printf("\"" + Cmd.Argv(1) + "\" isn't a valid key\n");
			return;
		}

		Key.SetBinding(b, null);
	}

	static xcommand_t Unbindall_f = new xcommand_t() {
		public void execute() {
			Key_Unbindall_f();
		}
	};

	static void Key_Unbindall_f() {
		for (int i = 0; i < 256; i++)
			Key.SetBinding(i, null);
	}

	static xcommand_t Bindlist_f = new xcommand_t() {
		public void execute() {
			Key_Bindlist_f();
		}
	};

	static void Key_Bindlist_f() {
		for (int i = 0; i < 256; i++)
			if (Globals.keybindings[i] != null && Globals.keybindings[i].length() != 0)
				Com.Printf(Key.KeynumToString(i) + " \"" + Globals.keybindings[i] + "\"\n");
	}

	static void ClearStates() {
		int i;

		Key.anykeydown = 0;

		for (i = 0; i < 256; i++) {
			if (keydown[i] || key_repeats[i]!=0)
				Event(i, false, 0);
			keydown[i] = false;
			key_repeats[i] = 0;
		}
	}

	public static void WriteBindings(RandomAccessFile f) {
		for (int i = 0; i < 256; i++)
			if (keybindings[i] != null && keybindings[i].length() > 0)
				try {
					f.writeBytes("bind " + KeynumToString(i) + " \"" + keybindings[i] + "\"\n");
				} catch (IOException e) {}
	}

}