Skip to content

Commit bc423ae

Browse files
author
systemblueteam
committed
feat: add VoiceOver accessibility labels to keyboard keys
1 parent 3b1a7bf commit bc423ae

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Sources/Keyboard/KeyboardKey.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ public struct KeyboardKey: View {
5353
var text: String
5454
var isActivatedExternally: Bool
5555

56+
/// Accessibility label that expands symbols to spoken words (e.g. "C 4", "F sharp 3")
57+
var accessibilityNoteLabel: String {
58+
let note = pitch.note(in: .C)
59+
let letter = note.noteClass.letter.description
60+
let octave = note.octave
61+
switch note.accidental {
62+
case .sharp:
63+
return "\(letter) sharp \(octave)"
64+
case .flat:
65+
return "\(letter) flat \(octave)"
66+
default:
67+
return "\(letter) \(octave)"
68+
}
69+
}
70+
5671
var keyColor: Color {
5772
if isActivatedExternally || isActivated {
5873
return pressedColor
@@ -121,6 +136,10 @@ public struct KeyboardKey: View {
121136
.foregroundColor(textColor)
122137
.padding(relativeFontSize(in: proxy.size) / 3.0)
123138
}
139+
.accessibilityElement(children: .ignore)
140+
.accessibilityLabel(accessibilityNoteLabel)
141+
.accessibilityAddTraits(.isButton)
142+
.accessibilityValue(isActivated || isActivatedExternally ? "pressed" : "not pressed")
124143
}
125144
}
126145
}

0 commit comments

Comments
 (0)