Package com.project.calculator
Class KeyboardEvents
java.lang.Object
com.project.calculator.KeyboardEvents
- All Implemented Interfaces:
KeyListener,EventListener
The
KeyboardEvents class handles keyboard input events for the calculator.
It listens for key presses and performs the appropriate actions, such as:
- Appending digits and operators to the display.
- Handling special keys (Enter, Backspace, Delete, and Escape).
- Supporting both standard and numpad keys.
- Displaying and calculating the result.
This class interacts with both BaseUI and MainUI instances
to update the display and process the input.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionKeyboardEvents(BaseUI baseUiInstance, MainUI mainUiInstance) Constructs theKeyboardEventsobject with references to the UI instances. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidHandles the evaluation of the expression and displays the result.voidHandles the key-pressed event and performs the corresponding action.voidHandles the key-released event.voidHandles the key-typed event.
-
Field Details
-
baseUiInstance
Reference to theBaseUIinstance for accessing display and history labels. -
mainUiInstance
Reference to theMainUIinstance for handling input operations.
-
-
Constructor Details
-
KeyboardEvents
Constructs theKeyboardEventsobject with references to the UI instances.- Parameters:
baseUiInstance- TheBaseUIinstance for managing the display and history.mainUiInstance- TheMainUIinstance for managing the input operations.
-
-
Method Details
-
keyTyped
Handles the key-typed event.This method is not used in the current implementation.
- Specified by:
keyTypedin interfaceKeyListener- Parameters:
e- TheKeyEventtriggered by typing a key.
-
keyPressed
Handles the key-pressed event and performs the corresponding action.This method processes the following:
- Digits, decimal points, and operators.
- Special keys (Enter, Backspace, Delete, and Escape).
- Handles both standard and numpad keys.
- Specified by:
keyPressedin interfaceKeyListener- Parameters:
e- TheKeyEventtriggered by pressing a key.
-
keyReleased
Handles the key-released event.This method is not used in the current implementation.
- Specified by:
keyReleasedin interfaceKeyListener- Parameters:
e- TheKeyEventtriggered by releasing a key.
-
handleResult
private void handleResult()Handles the evaluation of the expression and displays the result.This method:
- Sets the history label with the current display content.
- Converts the infix expression to postfix using
InfixToPostfixclass. - Displays the evaluated result.
- Clears the input and updates it with the result.
-