티스토리 뷰
🍏/iOS
[UIKeyCommand] UIKeyCommand로 키보드의 방향키(Up, Down, Left, Right)를 인지해보자
eungding 2020. 5. 11. 22:31728x90
반응형
UIKeyCommand 는 하드웨어 키보드에서 수행되는 키 누름과 그에 따른 동작을 지정하는 객체라고 합니다.
이것을 통해서 키보드의 방향키를 인지하게 할 수 도 있고 원하는 단축어를 만들 수도 있는 것 같아요
다양하게 많이 할 수 있지만, 우선 오늘 알게 된 키보드 화살표 키를 인지하는 것을 간단히 정리-!!
1. keyCommands를 오버라이딩 하기
keycommands를 오버라이딩 하여서 원하는 keyCommand와 동작을 지정해줄 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
override var keyCommands: [UIKeyCommand]? { | |
return [ | |
UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(keyboardUpArrowDidPressed)), | |
UIKeyCommand(input: UIKeyCommand.inputDownArrow, modifierFlags: [], action: #selector(keyboardDownArrowDidPressed)), | |
UIKeyCommand(input: UIKeyCommand.inputLeftArrow, modifierFlags: [], action: #selector(keyboardLeftArrowDidPressed)), | |
UIKeyCommand(input: UIKeyCommand.inputRightArrow, modifierFlags: [], action: #selector(keyboardRightArrowDidPressed)) | |
] | |
} | |
@objc | |
private func keyboardUpArrowDidPressed() { | |
print("⬆️") | |
} | |
@objc | |
private func keyboardDownArrowDidPressed() { | |
print("⬇️") | |
} | |
@objc | |
private func keyboardLeftArrowDidPressed() { | |
print("⬅️") | |
} | |
@objc | |
private func keyboardRightArrowDidPressed() { | |
print("➡️") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
} |
2. addKeyCommand 하기

UIViewController의 addKeyCommand 함수를 이용해서 원하는 keyCommand와 동작을 지정해줄 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.addKeyCommand(UIKeyCommand(input: UIKeyCommand.inputUpArrow, | |
modifierFlags: [], | |
action: #selector(keyboardUpArrowDidPressed))) | |
self.addKeyCommand(UIKeyCommand(input: UIKeyCommand.inputDownArrow, | |
modifierFlags: [], | |
action: #selector(keyboardDownArrowDidPressed))) | |
} |
[다른 키 + 방향키를 인지하고 싶다면]
그럴땐 modifierFlags를 이용해주면 됩니다-!!
ex) Shift 키 + 화살표 UP키를 인지하고 싶을 때
UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: .shift, action: #selector(keyboardUpArrowDidPressed))
ex) Command 키 + Shift 키 + 화살표 UP키를 인지하고 싶을 때
UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [.command, .shift], action: #selector(keyboardUpArrowDidPressed))
[유의사항]
키보드 방향키 관련 keyCommand를 쓰는 viewController에 textView를 올리면
textView에서 방향키를 누르면 자동으로 커서 이동해주는 것이 안먹힙니다-!!

반응형
'🍏 > iOS' 카테고리의 다른 글
[CIKernel] CIColorKernel 에 대하여 알아보자 (0) | 2020.06.12 |
---|---|
[CoreImage] 기본 필터 & 커스텀 필터 (OpenGL, Metal) 적용하기 (0) | 2020.06.12 |
[UINavigationItem] autolayout을 이용하여 navigation titleView 만들기 (0) | 2020.05.05 |
[SceneDelegate] 멀티 윈도우로 파악해보는 SceneDelegate 메소드들 (1) | 2020.04.26 |
[App's Life Cycle] App-Based Life-Cycle과 Scene-Based Life-Cycle (2) | 2020.04.26 |
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Sketch 누끼
- Flutter Text Gradient
- Flutter getter setter
- cocoapod
- flutter deep link
- 장고 URL querystring
- METAL
- Flutter 로딩
- DRF APIException
- Python Type Hint
- Dart Factory
- 구글 Geocoding API
- ipad multitasking
- 장고 Custom Management Command
- github actions
- ribs
- drf custom error
- Django FCM
- 플러터 싱글톤
- SerializerMethodField
- Flutter Spacer
- Watch App for iOS App vs Watch App
- flutter 앱 출시
- 플러터 얼럿
- Django Heroku Scheduler
- Flutter Clipboard
- flutter build mode
- PencilKit
- flutter dynamic link
- Django Firebase Cloud Messaging
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함