1 using System;
2 using LibRender2.Screens;
3 using OpenTK.Input;
4 using OpenBveApi.Interface;
5 
6 namespace OpenBve
7 {
8 	internal static partial class MainLoop
9 	{
10 		/// <summary>Called when a KeyDown event is generated</summary>
keyDownEvent(object sender, KeyboardKeyEventArgs e)11 		internal static void keyDownEvent(object sender, KeyboardKeyEventArgs e)
12 		{
13 			if (Interface.CurrentOptions.KioskMode)
14 			{
15 				//If in kiosk mode, reset the timer and disable AI on keypress
16 				MainLoop.kioskModeTimer = 0;
17 				TrainManager.PlayerTrain.AI = null;
18 			}
19 			if (Loading.Complete == true && e.Key == OpenTK.Input.Key.F4 && e.Alt == true)
20 			{
21 				// Catch standard ALT + F4 quit and push confirmation prompt
22 				Game.Menu.PushMenu(MenuType.Quit);
23 				return;
24 			}
25 			BlockKeyRepeat = true;
26 			//Check for modifiers
27 			if (e.Shift) CurrentKeyboardModifier |= KeyboardModifier.Shift;
28 			if (e.Control) CurrentKeyboardModifier |= KeyboardModifier.Ctrl;
29 			if (e.Alt) CurrentKeyboardModifier |= KeyboardModifier.Alt;
30 			if (Program.Renderer.CurrentInterface == InterfaceType.Menu && Game.Menu.IsCustomizingControl())
31 			{
32 				Game.Menu.SetControlKbdCustomData((OpenBveApi.Input.Key)e.Key, CurrentKeyboardModifier);
33 				return;
34 			}
35 			//Traverse the controls array
36 			for (int i = 0; i < Interface.CurrentControls.Length; i++)
37 			{
38 				//If we're using keyboard for this input
39 				if (Interface.CurrentControls[i].Method == ControlMethod.Keyboard)
40 				{
41 					//Compare the current and previous keyboard states
42 					//Only process if they are different
43 					if (!Enum.IsDefined(typeof(OpenBveApi.Input.Key), Interface.CurrentControls[i].Key)) continue;
44 					if ((OpenBveApi.Input.Key)e.Key == Interface.CurrentControls[i].Key & Interface.CurrentControls[i].Modifier == CurrentKeyboardModifier)
45 					{
46 
47 						Interface.CurrentControls[i].AnalogState = 1.0;
48 						Interface.CurrentControls[i].DigitalState = DigitalControlState.Pressed;
49 						//Key repeats should not be added in non-game interface modes, unless they are Menu Up/ Menu Down commands
50 						if (Program.Renderer.CurrentInterface == InterfaceType.Normal || Interface.CurrentControls[i].Command == Translations.Command.MenuUp || Interface.CurrentControls[i].Command == Translations.Command.MenuDown)
51 						{
52 							if (Interface.CurrentControls[i].Command == Translations.Command.CameraInterior |
53 								Interface.CurrentControls[i].Command == Translations.Command.CameraExterior |
54 								Interface.CurrentControls[i].Command == Translations.Command.CameraFlyBy |
55 								Interface.CurrentControls[i].Command == Translations.Command.CameraTrack)
56 							{
57 								//HACK: We don't want to bounce between camera modes when holding down the mode switch key
58 								continue;
59 							}
60 							AddControlRepeat(i);
61 						}
62 					}
63 				}
64 			}
65 			BlockKeyRepeat = false;
66 			//Remember to reset the keyboard modifier after we're done, else it repeats.....
67 			CurrentKeyboardModifier = KeyboardModifier.None;
68 		}
69 
70 		/// <summary>Called when a KeyUp event is generated</summary>
keyUpEvent(object sender, KeyboardKeyEventArgs e)71 		internal static void keyUpEvent(object sender, KeyboardKeyEventArgs e)
72 		{
73 			if (Interface.CurrentOptions.KioskMode)
74 			{
75 				//If in kiosk mode, reset the timer and disable AI on keypress
76 				MainLoop.kioskModeTimer = 0;
77 				TrainManager.PlayerTrain.AI = null;
78 			}
79 			if (Program.Renderer.PreviousInterface == InterfaceType.Menu & Program.Renderer.CurrentInterface == InterfaceType.Normal)
80 			{
81 				//Set again to block the first keyup event after the menu has been closed, as this may produce unwanted effects
82 				//if the menu select key is also mapped in-game
83 				Program.Renderer.CurrentInterface = InterfaceType.Normal;
84 				return;
85 			}
86 			//We don't need to check for modifiers on key up
87 			BlockKeyRepeat = true;
88 			//Traverse the controls array
89 			for (int i = 0; i < Interface.CurrentControls.Length; i++)
90 			{
91 				//If we're using keyboard for this input
92 				if (Interface.CurrentControls[i].Method == ControlMethod.Keyboard)
93 				{
94 					//Compare the current and previous keyboard states
95 					//Only process if they are different
96 					if (!Enum.IsDefined(typeof(OpenBveApi.Input.Key), Interface.CurrentControls[i].Key)) continue;
97 					if ((OpenBveApi.Input.Key)e.Key == Interface.CurrentControls[i].Key & Interface.CurrentControls[i].AnalogState == 1.0 & Interface.CurrentControls[i].DigitalState > DigitalControlState.Released)
98 					{
99 						Interface.CurrentControls[i].AnalogState = 0.0;
100 						Interface.CurrentControls[i].DigitalState = DigitalControlState.Released;
101 						RemoveControlRepeat(i);
102 					}
103 				}
104 			}
105 			BlockKeyRepeat = false;
106 		}
107 	}
108 }
109