Skip to content

Commit cffb7da

Browse files
authored
Merge pull request #42 from hdcola/hdcola
add keyboardShortcuts Tap
2 parents 56b4871 + a936856 commit cffb7da

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//
2+
// SwiftUIView.swift
3+
//
4+
//
5+
// Created by 老房东 on 2022-09-12.
6+
//
7+
8+
import SwiftUI
9+
10+
struct KeyboardShortcutsControlView: View {
11+
12+
var body: some View {
13+
ScrollView{
14+
VStack{
15+
HeadlineView(
16+
title: "Keyboard Shortcuts",
17+
url: "https://developer.apple.com/documentation/swiftui/view/keyboardshortcut(_:)-8liec",
18+
description: "Assigns a keyboard shortcut to the modified control."
19+
)
20+
KeyboardShortcutsSampleView()
21+
Divider()
22+
GameControlSampleView()
23+
}
24+
.padding()
25+
}
26+
}
27+
}
28+
29+
private struct GameControlSampleView: View {
30+
var code = """
31+
struct GameControlSampleView: View {
32+
@State var x = 0.0
33+
@State var y = 0.0
34+
@State var degress = 0.0
35+
var body: some View {
36+
VStack{
37+
ZStack{
38+
Rectangle()
39+
.fill(.cyan)
40+
.frame(width: 50,height: 50)
41+
.offset(x:x,y:y)
42+
.rotationEffect(.degrees(degress))
43+
}
44+
.frame(width: 300,height: 300)
45+
.border(.black)
46+
HStack{
47+
Button("⬅️"){
48+
withAnimation {
49+
x -= 5
50+
}
51+
}.keyboardShortcut(.leftArrow, modifiers: [])
52+
Button("➡️"){
53+
withAnimation {
54+
x += 5
55+
}
56+
}.keyboardShortcut(.rightArrow, modifiers: [])
57+
Button("⬆️"){
58+
withAnimation {
59+
y -= 5
60+
}
61+
}.keyboardShortcut(.upArrow, modifiers: [])
62+
Button("⬇️"){
63+
withAnimation {
64+
y += 5
65+
}
66+
}.keyboardShortcut(.downArrow, modifiers: [])
67+
Button("Space🔄"){
68+
withAnimation {
69+
degress -= 10
70+
}
71+
}.keyboardShortcut(.space, modifiers: [])
72+
Button("Enter⏹"){
73+
withAnimation {
74+
x = 0
75+
y = 0
76+
degress = 0
77+
}
78+
}.keyboardShortcut(.return, modifiers: [])
79+
}
80+
}
81+
}
82+
}
83+
"""
84+
@State var x = 0.0
85+
@State var y = 0.0
86+
@State var degress = 0.0
87+
var body: some View {
88+
VStack{
89+
Text("Move Game")
90+
.font(.title2)
91+
CodePreviewView(code: code)
92+
ZStack{
93+
Rectangle()
94+
.fill(.cyan)
95+
.frame(width: 50,height: 50)
96+
.offset(x:x,y:y)
97+
.rotationEffect(.degrees(degress))
98+
}
99+
.frame(width: 300,height: 300)
100+
.border(.black)
101+
HStack{
102+
Button("⬅️"){
103+
withAnimation {
104+
x -= 5
105+
}
106+
}.keyboardShortcut(.leftArrow, modifiers: [])
107+
Button("➡️"){
108+
withAnimation {
109+
x += 5
110+
}
111+
}.keyboardShortcut(.rightArrow, modifiers: [])
112+
Button("⬆️"){
113+
withAnimation {
114+
y -= 5
115+
}
116+
}.keyboardShortcut(.upArrow, modifiers: [])
117+
Button("⬇️"){
118+
withAnimation {
119+
y += 5
120+
}
121+
}.keyboardShortcut(.downArrow, modifiers: [])
122+
Button("Space🔄"){
123+
withAnimation {
124+
degress -= 10
125+
}
126+
}.keyboardShortcut(.space, modifiers: [])
127+
Button("Enter⏹"){
128+
withAnimation {
129+
x = 0
130+
y = 0
131+
degress = 0
132+
}
133+
}.keyboardShortcut(.return, modifiers: [])
134+
}
135+
}
136+
}
137+
}
138+
139+
private struct KeyboardShortcutsSampleView: View {
140+
var code = """
141+
Button("a"){
142+
inputKey = "a"
143+
}.keyboardShortcut("A",modifiers: [])
144+
Button("B"){
145+
inputKey = "b"
146+
}.keyboardShortcut("B",modifiers: [.shift])
147+
Button("b"){
148+
inputKey = "b"
149+
}.keyboardShortcut("B",modifiers: [])
150+
Button("c"){
151+
inputKey = "c"
152+
}
153+
.hidden()
154+
.keyboardShortcut("c",modifiers: [])
155+
"""
156+
@State var inputKey = ""
157+
var body: some View {
158+
VStack{
159+
CodePreviewView(code: code)
160+
Text("Input :\(inputKey)")
161+
HStack{
162+
Button("a"){
163+
inputKey = "a"
164+
}.keyboardShortcut("A",modifiers: [])
165+
Button("B"){
166+
inputKey = "B"
167+
}.keyboardShortcut("B",modifiers: [.shift])
168+
Button("b"){
169+
inputKey = "b"
170+
}.keyboardShortcut("B",modifiers: [])
171+
Button("c"){
172+
inputKey = "c"
173+
}
174+
.hidden()
175+
.keyboardShortcut("c",modifiers: [])
176+
}
177+
}
178+
}
179+
}
180+
181+
struct KeyboardShortcutsControlView_Previews: PreviewProvider {
182+
static var previews: some View {
183+
KeyboardShortcutsControlView()
184+
}
185+
}

OneTapSwiftUI.swiftpm/Sidebar.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ struct Sidebar: View {
4848
}
4949
}
5050

51+
Section("Input and event modifiers"){
52+
NavigationLink("Keyboard shortcuts"){
53+
KeyboardShortcutsControlView()
54+
}
55+
}
56+
5157
Section("Map"){
5258
NavigationLink("Map"){
5359
MapControlView()

0 commit comments

Comments
 (0)