Godot基础教程07:输入映射与事件

输入映射

我们在第4节时,使用了一行代码来判断玩家是否按下某个键:

#是否按下左键
if Input.is_action_pressed("ui_left"):

其中,Input类是专门用于处理输入的类,包含鼠标、键盘、手柄的输入。is_action_pressed是检查按个输入映射是否处于正在按下状态。
通过代码可以看到,我们检查了"ui_left"是否按下,那么,这个"ui_left"是如何定义的?

在菜单栏找到项目项目设置,打开如下界面:

项目设置.png

在项目设置中找到键位映射,接下来,即使我不说,你也应该能猜到这个功能如何使用了。所以这里留给大家自行研究。

输入事件

在第四节中,为了检测空格键发射子弹,我用了以下代码:

#检测发射子弹
if Input.is_action_pressed("ui_select") and cd_timer < 0:

在这里,我使用了自己记录cd的方式来避免玩家一直按着空格,导致每帧发射一个子弹的问题,实际上有更好的解决方案。
接下来我们认识三个检测输入事件的函数:

if Input.is_action_just_pressed(action):
    #只有当按键按下那一帧才会判断通过
    print("你按下了某键")
if Input.is_action_just_released(action):
    #只有当按键释放那一帧才会判断通过
    print("你放开了某键")
if Input.is_action_pressed(action):
    #只要按键处于按下状态,每一帧都会判断通过
    print("你按着某键不松手")

以上三个检测函数,都需要首先在按键映射里事先定义,或者直接使用按钮映射里默认的映射。
除此之外,还有两个函数也比较常用:

if Input.is_key_pressed(scancode):
    #某一按键处于按下状态,scancode是按键编号
    pass
if Input.is_mouse_button_pressed(button):
    #按下鼠标某个按钮,button是按钮编码
    pass

附:鼠标键盘按钮编码(很多键我也没有见过,可能是专业级别的设备才有吧)

#鼠标按钮编码
enum  ButtonList:
    BUTTON_LEFT = 1#鼠标左键
    BUTTON_RIGHT = 2#鼠标右键
    BUTTON_MIDDLE = 3#鼠标中键
    BUTTON_XBUTTON1 = 8#额外的鼠标按钮1(仅在某些鼠标上存在)
    BUTTON_XBUTTON2 = 9#额外的鼠标按钮2(仅在某些鼠标上存在)
    BUTTON_WHEEL_UP = 4#鼠标滚轮向上
    BUTTON_WHEEL_DOWN = 5#鼠标滚轮向下
    BUTTON_WHEEL_LEFT = 6#鼠标滚轮左键(仅在某些鼠标上存在)
    BUTTON_WHEEL_RIGHT = 7#鼠标滚轮右键(仅在某些鼠标上存在)
    BUTTON_MASK_LEFT = 1#鼠标左键蒙版
    BUTTON_MASK_RIGHT = 2#鼠标右键蒙版
    BUTTON_MASK_MIDDLE = 4#鼠标中键蒙版
    BUTTON_MASK_XBUTTON1 = 128#额外的鼠标按钮1个遮罩
    BUTTON_MASK_XBUTTON2 = 256#额外的鼠标按钮2遮罩

#键盘按键编码
enum  KeyList:
    KEY_ESCAPE = 16777217#Esc键
    KEY_TAB = 16777218#Tab键
    KEY_BACKTAB = 16777219#Shift + Tab键
    KEY_BACKSPACE = 16777220#退格键
    KEY_ENTER = 16777221#主键盘Enter键
    KEY_KP_ENTER = 16777222#数字键盘Enter键
    KEY_INSERT = 16777223#Insert键
    KEY_DELETE = 16777224#Delete键
    KEY_PAUSE = 16777225#Pause键
    KEY_PRINT = 16777226#打印屏幕键
    KEY_SYSREQ = 16777227#系统请求密钥
    KEY_CLEAR = 16777228#Clear键
    KEY_HOME = 16777229#Home键
    KEY_END = 16777230#End键
    KEY_LEFT = 16777231#左箭头键
    KEY_UP = 16777232#向上箭头键
    KEY_RIGHT = 16777233#向右箭头键
    KEY_DOWN = 16777234#向下箭头键
    KEY_PAGEUP = 16777235#Page Up键
    KEY_PAGEDOWN = 16777236#Page Down键
    KEY_SHIFT = 16777237#Shift键
    KEY_CONTROL = 16777238#控制键
    KEY_META = 16777239#中继键
    KEY_ALT = 16777240#ALT键
    KEY_CAPSLOCK = 16777241#Caps Lock键
    KEY_NUMLOCK = 16777242#Num Lock键
    KEY_SCROLLLOCK = 16777243#滚动锁定键
    KEY_F1 = 16777244#F1键
    KEY_F2 = 16777245#F2键
    KEY_F3 = 16777246#F3键
    KEY_F4 = 16777247#F4键
    KEY_F5 = 16777248#F5键
    KEY_F6 = 16777249#F6键
    KEY_F7 = 16777250#F7键
    KEY_F8 = 16777251#F8键
    KEY_F9 = 16777252#F9键
    KEY_F10 = 16777253#F10键
    KEY_F11 = 16777254#F11键
    KEY_F12 = 16777255#F12键
    KEY_F13 = 16777256#F13键
    KEY_F14 = 16777257#F14键
    KEY_F15 = 16777258#F15键
    KEY_F16 = 16777259#F16键
    KEY_KP_MULTIPLY = 16777345#数字小键盘上的(*)键
    KEY_KP_DIVIDE = 16777346#数字键盘上的(/)键
    KEY_KP_SUBTRACT = 16777347#数字键盘上的减号(-)键
    KEY_KP_PERIOD = 16777348#数字键盘上的句点(.)键
    KEY_KP_ADD = 16777349#在数字键盘上添加(+)键
    KEY_KP_0 = 16777350#数字键盘上的数字0
    KEY_KP_1 = 16777351#数字键盘上的数字1
    KEY_KP_2 = 16777352#数字键盘上的数字2
    KEY_KP_3 = 16777353#数字键盘上的数字3
    KEY_KP_4 = 16777354#数字键盘上的数字4
    KEY_KP_5 = 16777355#数字键盘上的数字5
    KEY_KP_6 = 16777356#数字键盘上的数字6
    KEY_KP_7 = 16777357#数字键盘上的数字7
    KEY_KP_8 = 16777358#数字键盘上的数字8
    KEY_KP_9 = 16777359#数字键盘上的数字9
    KEY_SUPER_L = 16777260#左超级键(Windows键)
    KEY_SUPER_R = 16777261#右超级键(Windows键)
    KEY_MENU = 16777262#上下文菜单键
    KEY_HYPER_L = 16777263#左超级键
    KEY_HYPER_R = 16777264#右超级键
    KEY_HELP = 16777265#帮助键
    KEY_DIRECTION_L = 16777266#左方向键
    KEY_DIRECTION_R = 16777267#右方向键
    KEY_BACK = 16777280#后退键
    KEY_FORWARD = 16777281#前进键
    KEY_STOP = 16777282#停止键
    KEY_REFRESH = 16777283#刷新密钥
    KEY_VOLUMEDOWN = 16777284#降低音量键
    KEY_VOLUMEMUTE = 16777285#静音键
    KEY_VOLUMEUP = 16777286#调高音量键
    KEY_BASSBOOST = 16777287#低音增强键
    KEY_BASSUP = 16777288#低音键
    KEY_BASSDOWN = 16777289#低音键
    KEY_TREBLEUP = 16777290#高音键
    KEY_TREBLEDOWN = 16777291#高音下移键
    KEY_MEDIAPLAY = 16777292#媒体播放键
    KEY_MEDIASTOP = 16777293#媒体停止键
    KEY_MEDIAPREVIOUS = 16777294#上一首歌曲的键
    KEY_MEDIANEXT = 16777295#下一个歌曲键
    KEY_MEDIARECORD = 16777296#媒体记录键
    KEY_HOMEPAGE = 16777297#主页键
    KEY_FAVORITES = 16777298#收藏夹键
    KEY_SEARCH = 16777299#搜索键
    KEY_STANDBY = 16777300#备用键
    KEY_OPENURL = 16777301#打开URL /启动浏览器键
    KEY_LAUNCHMAIL = 16777302#启动邮件密钥
    KEY_LAUNCHMEDIA = 16777303#启动媒体密钥
    KEY_LAUNCH0 = 16777304#启动快捷方式0键
    KEY_LAUNCH1 = 16777305#启动快捷方式1键
    KEY_LAUNCH2 = 16777306#启动快捷方式2键
    KEY_LAUNCH3 = 16777307#启动快捷键3键
    KEY_LAUNCH4 = 16777308#启动快捷方式4键
    KEY_LAUNCH5 = 16777309#启动快捷键5键
    KEY_LAUNCH6 = 16777310#启动快捷键6键
    KEY_LAUNCH7 = 16777311#启动快捷键7键
    KEY_LAUNCH8 = 16777312#启动快捷键8键
    KEY_LAUNCH9 = 16777313#启动快捷方式9键
    KEY_LAUNCHA = 16777314#启动快捷方式A键
    KEY_LAUNCHB = 16777315#启动快捷键B
    KEY_LAUNCHC = 16777316#启动快捷方式C键
    KEY_LAUNCHD = 16777317#启动快捷方式D键
    KEY_LAUNCHE = 16777318#启动快捷键E
    KEY_LAUNCHF = 16777319#启动快捷键F键
    KEY_UNKNOWN = 33554431#未知密钥
    KEY_SPACE = 32#空格键
    KEY_EXCLAM = 33#! key
    KEY_QUOTEDBL = 34#" key
    KEY_NUMBERSIGN = 35## key
    KEY_DOLLAR = 36#$ key
    KEY_PERCENT = 37#% key
    KEY_AMPERSAND = 38#& key
    KEY_APOSTROPHE = 39# key
    KEY_PARENLEFT = 40#( key
    KEY_PARENRIGHT = 41#) key
    KEY_ASTERISK = 42#退出键
    KEY_PLUS = 43#+ key
    KEY_COMMA = 44#, key
    KEY_MINUS = 45#- key
    KEY_PERIOD = 46#.key
    KEY_SLASH = 47#/ key
    KEY_0 = 48#Number 0
    KEY_1 = 49#Number 1
    KEY_2 = 50#Number 2
    KEY_3 = 51#Number 3
    KEY_4 = 52#Number 4
    KEY_5 = 53#Number 5
    KEY_6 = 54#Number 6
    KEY_7 = 55#Number 7
    KEY_8 = 56#Number 8
    KEY_9 = 57#Number 9
    KEY_COLON = 58#: key
    KEY_SEMICOLON = 59#; key
    KEY_LESS = 60#< key
    KEY_EQUAL = 61#= KEY
    KEY_GREATER = 62#> key
    KEY_QUESTION = 63#! key
    KEY_AT = 64#@ key
    KEY_A = 65#A key
    KEY_B = 66#B key
    KEY_C = 67#C key
    KEY_D = 68#D key
    KEY_E = 69#E key
    KEY_F = 70#F key
    KEY_G = 71#G key
    KEY_H = 72#H key
    KEY_I = 73#I key
    KEY_J = 74#J key
    KEY_K = 75#K key
    KEY_L = 76#L key
    KEY_M = 77#M key
    KEY_N = 78#N key
    KEY_O = 79#O key
    KEY_P = 80#P key
    KEY_Q = 81#Q key
    KEY_R = 82#R key
    KEY_S = 83#S key
    KEY_T = 84#T key
    KEY_U = 85#U key
    KEY_V = 86#V key
    KEY_W = 87#W key
    KEY_X = 88#X key
    KEY_Y = 89#Y key
    KEY_Z = 90#Z key
    KEY_BRACKETLEFT = 91#[ key
    KEY_BACKSLASH = 92#\ key
    KEY_BRACKETRIGHT = 93#] key
    KEY_ASCIICIRCUM = 94#^ key
    KEY_UNDERSCORE = 95#_ key
    KEY_QUOTELEFT = 96#` key
    KEY_BRACELEFT = 123#{ key
    KEY_BAR = 124#| key
    KEY_BRACERIGHT = 125#} key
    KEY_ASCIITILDE = 126#~ key
    KEY_NOBREAKSPACE = 160#不可破坏的空格键
    KEY_EXCLAMDOWN = 161#¡ key
    KEY_CENT = 162#¢ key
    KEY_STERLING = 163#£ key
    KEY_CURRENCY = 164#¤ key
    KEY_YEN = 165#¥ key
    KEY_BROKENBAR = 166#¦ key
    KEY_SECTION = 167#§ key
    KEY_DIAERESIS = 168#¨ key
    KEY_COPYRIGHT = 169#© key
    KEY_ORDFEMININE = 170#ª key
    KEY_GUILLEMOTLEFT = 171#« key
    KEY_NOTSIGN = 172#¬ key
    KEY_HYPHEN = 173#软连字符键
    KEY_REGISTERED = 174#® key
    KEY_MACRON = 175#¯ key
    KEY_DEGREE = 176#° key
    KEY_PLUSMINUS = 177#± key
    KEY_TWOSUPERIOR = 178#² key
    KEY_THREESUPERIOR = 179#³ key
    KEY_ACUTE = 180#´ key
    KEY_MU = 181#µ key
    KEY_PARAGRAPH = 182#¶ key
    KEY_PERIODCENTERED = 183#· key
    KEY_CEDILLA = 184#¸ key
    KEY_ONESUPERIOR = 185#¹ key
    KEY_MASCULINE = 186#º key
    KEY_GUILLEMOTRIGHT = 187#» key
    KEY_ONEQUARTER = 188#¼ key
    KEY_ONEHALF = 189#½ key
    KEY_THREEQUARTERS = 190#¾ key
    KEY_QUESTIONDOWN = 191#¿ key
    KEY_AGRAVE = 192#À key
    KEY_AACUTE = 193#Á key
    KEY_ACIRCUMFLEX = 194#Â key
    KEY_ATILDE = 195#Ã key
    KEY_ADIAERESIS = 196#Ä key
    KEY_ARING = 197#Å key
    KEY_AE = 198#Æ key
    KEY_CCEDILLA = 199#Ç key
    KEY_EGRAVE = 200#È key
    KEY_EACUTE = 201#É key
    KEY_ECIRCUMFLEX = 202#Ê key
    KEY_EDIAERESIS = 203#Ë key
    KEY_IGRAVE = 204#Ì key
    KEY_IACUTE = 205#Í key
    KEY_ICIRCUMFLEX = 206#Î key
    KEY_IDIAERESIS = 207#Ï key
    KEY_ETH = 208#Ð key
    KEY_NTILDE = 209#Ñ key
    KEY_OGRAVE = 210#Ò key
    KEY_OACUTE = 211#Ó key
    KEY_OCIRCUMFLEX = 212#Ô key
    KEY_OTILDE = 213#Õ key
    KEY_ODIAERESIS = 214#Ö key
    KEY_MULTIPLY = 215#× key
    KEY_OOBLIQUE = 216#Ø key
    KEY_UGRAVE = 217#Ù key
    KEY_UACUTE = 218#Ú key
    KEY_UCIRCUMFLEX = 219#Û key
    KEY_UDIAERESIS = 220#Ü key
    KEY_YACUTE = 221#Ý key
    KEY_THORN = 222#Þ key
    KEY_SSHARP = 223#ß key.KEY_DIVISION = 247#÷ key
    KEY_YDIAERESIS = 255#ÿ key

你可能感兴趣的:(Godot基础教程07:输入映射与事件)