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 theKeyboardEvents
object with references to the UI instances. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Handles the evaluation of the expression and displays the result.void
Handles the key-pressed event and performs the corresponding action.void
Handles the key-released event.void
Handles the key-typed event.
-
Field Details
-
baseUiInstance
Reference to theBaseUI
instance for accessing display and history labels. -
mainUiInstance
Reference to theMainUI
instance for handling input operations.
-
-
Constructor Details
-
KeyboardEvents
Constructs theKeyboardEvents
object with references to the UI instances.- Parameters:
baseUiInstance
- TheBaseUI
instance for managing the display and history.mainUiInstance
- TheMainUI
instance for managing the input operations.
-
-
Method Details
-
keyTyped
Handles the key-typed event.This method is not used in the current implementation.
- Specified by:
keyTyped
in interfaceKeyListener
- Parameters:
e
- TheKeyEvent
triggered 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:
keyPressed
in interfaceKeyListener
- Parameters:
e
- TheKeyEvent
triggered by pressing a key.
-
keyReleased
Handles the key-released event.This method is not used in the current implementation.
- Specified by:
keyReleased
in interfaceKeyListener
- Parameters:
e
- TheKeyEvent
triggered 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
InfixToPostfix
class. - Displays the evaluated result.
- Clears the input and updates it with the result.
-