macOS 监听键盘输入的正确方式

macOS 监听键盘输入的正确方式,第1张

macOS开发因为硬件上有鼠标和键盘的支持,所以我们也有监听硬件输入的需求。

监听键盘输入需要使用 NSEvent 这个类,这个类是专门管理“事件”的。 NSEvent 提供了两个监听的方法:

第一个方法是用于监听其他应用中的事件,第二个是用于监听自己应用的时间。更多详细内容可以阅读官方注释。

在这里我们使用第二个进行监听键盘输入:

在方法的 block 回调中会收到当前键盘的事件,你需要通过判断 aEvent.keyCode 来得知点击了哪一个键。macOS有一个枚举来告诉你键的名字是什么,你可以直接拿来判断(文末附)。

NSEvent 的监听是需要进行移除的 ,如果你不进行移除,那么这个需要监听的类再多次 init 后,其 block 也会在键盘点击时被重复的调用多次。

移除需要使用的类为 NSEvent ,方法为:

如何监听 那一节,添加监听的方法有一个返回值,这个返回值就是这里 removeMonitor 需要传入的参数。在添加了监听的对象 dealloc 或合适的时候进行调用即可。

键盘的每个键都有对应的事件值,用按键按下或抬起事件,就可以绑定了,事件和对应值如下:

1、keydown()

keydown事件会在键盘按下时触发.

2、keyup()

keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件

3、keypress()

keypress事件会在敲击按键时触发,我们可以理解为按下并抬起同一个按键

对应值:

keycode 8 = BackSpace BackSpace

keycode 9 = Tab Tab

keycode12 = Clear

keycode13 = Enter

keycode16 = Shift_L

keycode17 = Control_L

keycode18 = Alt_L

keycode19 = Pause

keycode20 = Caps_Lock

keycode27 = Escape Escape

keycode32 = space space

keycode33 = Prior

keycode34 = Next

keycode35 = End

keycode36 = Home

keycode37 = Left

keycode38 = Up

keycode39 = Right

keycode40 = Down

keycode41 = Select

keycode42 = Print

keycode43 = Execute

keycode45 = Insert

keycode46 = Delete

keycode47 = Help

keycode48 = 0 equal braceright

keycode49 = 1 exclam onesuperior

keycode50 = 2 quotedbl twosuperior

keycode51 = 3 section threesuperior

keycode52 = 4 dollar

keycode53 = 5 percent

keycode54 = 6 ampersand

keycode55 = 7 slash braceleft

keycode56 = 8 parenleft bracketleft

keycode57 = 9 parenright bracketright

keycode65 = a A

keycode66 = b B

keycode67 = c C

keycode68 = d D

keycode69 = e E EuroSign

keycode70 = f F

keycode71 = g G

keycode72 = h H

keycode73 = i I

keycode74 = j J

keycode75 = k K

keycode76 = l L

keycode77 = m M mu

keycode78 = n N

keycode79 = o O

keycode80 = p P

keycode81 = q Q at

keycode82 = r R

keycode83 = s S

keycode84 = t T

keycode85 = u U

keycode86 = v V

keycode87 = w W

keycode88 = x X

keycode89 = y Y

keycode90 = z Z

keycode96 = KP_0 KP_0

keycode97 = KP_1 KP_1

keycode98 = KP_2 KP_2

keycode99 = KP_3 KP_3

keycode 100 = KP_4 KP_4

keycode 101 = KP_5 KP_5

keycode 102 = KP_6 KP_6

keycode 103 = KP_7 KP_7

keycode 104 = KP_8 KP_8

keycode 105 = KP_9 KP_9

keycode 106 = KP_Multiply KP_Multiply

keycode 107 = KP_Add KP_Add

keycode 108 = KP_Separator KP_Separator

keycode 109 = KP_Subtract KP_Subtract

keycode 110 = KP_Decimal KP_Decimal

keycode 111 = KP_Divide KP_Divide

keycode 112 = F1

keycode 113 = F2

keycode 114 = F3

keycode 115 = F4

keycode 116 = F5

keycode 117 = F6

keycode 118 = F7

keycode 119 = F8

keycode 120 = F9

keycode 121 = F10

keycode 122 = F11

keycode 123 = F12

keycode 124 = F13

keycode 125 = F14

keycode 126 = F15

keycode 127 = F16

keycode 128 = F17

keycode 129 = F18

keycode 130 = F19

keycode 131 = F20

keycode 132 = F21

keycode 133 = F22

keycode 134 = F23

keycode 135 = F24

keycode 136 = Num_Lock

keycode 137 = Scroll_Lock

keycode 187 = acute grave

keycode 188 = comma semicolon

keycode 189 = minus underscore

keycode 190 = period colon

keycode 192 = numbersign apostrophe

keycode 210 = plusminus hyphen macron

keycode 211 =

keycode 212 = copyright registered

keycode 213 = guillemotleft guillemotright

keycode 214 = masculine ordfeminine

keycode 215 = ae AE

keycode 216 = cent yen

keycode 217 = questiondown exclamdown

keycode 218 = onequarter onehalf threequarters

keycode 220 = less greater bar

keycode 221 = plus asterisk asciitilde

keycode 227 = multiply division

keycode 228 = acircumflex Acircumflex

keycode 229 = ecircumflex Ecircumflex

keycode 230 = icircumflex Icircumflex

keycode 231 = ocircumflex Ocircumflex

keycode 232 = ucircumflex Ucircumflex

keycode 233 = ntilde Ntilde

keycode 234 = yacute Yacute

keycode 235 = oslash Ooblique

keycode 236 = aring Aring

keycode 237 = ccedilla Ccedilla

keycode 238 = thorn THORN

keycode 239 = eth ETH

keycode 240 = diaeresis cedilla currency

keycode 241 = agrave Agrave atilde Atilde

keycode 242 = egrave Egrave

keycode 243 = igrave Igrave

keycode 244 = ograve Ograve otilde Otilde

keycode 245 = ugrave Ugrave

keycode 246 = adiaeresis Adiaeresis

keycode 247 = ediaeresis Ediaeresis

keycode 248 = idiaeresis Idiaeresis

keycode 249 = odiaeresis Odiaeresis

keycode 250 = udiaeresis Udiaeresis

keycode 251 = ssharp question backslash

keycode 252 = asciicircum degree

keycode 253 = 3 sterling

keycode 254 = Mode_switch

importjava.awt.*importjava.awt.event.*publicclassKeyTestextendsFrame{publicKeyTest(){//注册键盘监听addKeyListener(newKeyAdapter(){publicvoidkeyPressed(KeyEventke){System.out.println("Thecharis:"+ke.getKeyChar())}})//注册窗口监听setSize(100,100)setVisible(true)}publicstaticvoidmain(String[]args){newKeyTest()}}不知道这个行否?


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11630933.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存