From 411549613dfda8e69b41842f0a1fb371f9546c3c Mon Sep 17 00:00:00 2001 From: David North Date: Fri, 25 Oct 2024 20:10:49 +0100 Subject: [PATCH] Adds `asNumeric` to `Key` class Fixes #746 --- .../scala/indigo/shared/constants/Key.scala | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/indigo/indigo/src/main/scala/indigo/shared/constants/Key.scala b/indigo/indigo/src/main/scala/indigo/shared/constants/Key.scala index 03c842d3b..14bc5d500 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/constants/Key.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/constants/Key.scala @@ -4,7 +4,7 @@ final case class Key(code: KeyCode, key: String, location: KeyLocation) derives def isPrintable: Boolean = (key != "") && KeyCode.printable.contains(this.code) - def isNumber: Boolean = + def isNumeric: Boolean = code match { case KeyCode.Digit0 | KeyCode.Digit1 | KeyCode.Digit2 | KeyCode.Digit3 | KeyCode.Digit4 | KeyCode.Digit5 | KeyCode.Digit6 | KeyCode.Digit7 | KeyCode.Digit8 | KeyCode.Digit9 => @@ -13,6 +13,21 @@ final case class Key(code: KeyCode, key: String, location: KeyLocation) derives false } + def asNumeric: Option[Int] = + code match { + case KeyCode.Digit0 => Some(0) + case KeyCode.Digit1 => Some(1) + case KeyCode.Digit2 => Some(2) + case KeyCode.Digit3 => Some(3) + case KeyCode.Digit4 => Some(4) + case KeyCode.Digit5 => Some(5) + case KeyCode.Digit6 => Some(6) + case KeyCode.Digit7 => Some(7) + case KeyCode.Digit8 => Some(8) + case KeyCode.Digit9 => Some(9) + case _ => None + } + // DO NOT REMOVE def ===(other: Key): Boolean = code == other.code