InputObject

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง

อินพุต InputObject แทนที่การใส่ของผู้ใช้คนเดียว เช่น การเคลื่อนไหวของเมาส์ การสัมผัส การกดปุ่ม และอื่นๆ สร้างขึ้นเมื่อการใส่เริ่มต้น

คุณสมบัติของวัตถุนี้แตกต่างกันตาม UserInputType .แต่ละประเภทของการใส่จะได้รับการเปลี่ยนแปลงต่างๆ ในส่วน UserInputState ของมันในระหว่างชีวิตของอินพุตอื่นๆ ที่อธิบายอินพุตเพิ่มเติมอาจเปลี่ยนแปลงได้ เช่น Position และ Deltaแป้นพิมพ์และปุ่มเกมแดดจัดจะมีค่าตั้งค่า KeyCode ที่กำหนด

เมื่อสร้างในตอนต้นของการป้อนข้อมูล วัตถุเดียวกันจะยังคงอยู่และอัปเดตจนกว่าการป้อนข้อมูลจะสิ้นสุดผลลัพธ์คือคุณสามารถติดตามการเปลี่ยนแปลงของวัตถุโดยใช้เหตุการณ์ Changed เมื่อผู้ใช้เปลี่ยนการใส่ข้อมูลในคำถามคุณยังสามารถวางวัตถุเหล่านี้ลงในรายการของสนามอินพุตที่ใช้งานและโต้ตอบกับวัตถุหลังจากสร้างโดยเหตุการณ์เช่น UserInputService.InputBeganส่วนใหญ่มันมีประโยชน์สำหรับเหตุการณ์แตะ เนื่องจากแต่ละจุดสัมผัสจะมีอินพุตที่แยกต่างหาก

ดูเพิ่ม:

  • ContextActionService ซึ่งส่ง InputObject ไปยังฟังก์ชันการจัดการการดำเนินการ bound
  • UserInputService ซึ่งอีเวนต์และฟังก์ชันมักใช้ InputObject
  • GuiObject ซึ่งเหตุการณ์ที่เกี่ยวข้องกับการใส่ข้อมูลของผู้ใช้ใช้ InputObject

ตัวอย่างโค้ด

The following example demonstrates one of many usage examples of handling user input from InputBegan depending on its type.

Handling InputBegan

-- In order to use the InputBegan event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)

The following example demonstrates one of many usage examples of handling user input from InputChanged depending on its type.

Handling InputChanged

-- In order to use the InputChanged event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- Prints the current input position and the change (delta) in position
local function printMovement(input)
print("Position:", input.Position)
print("Movement Delta:", input.Delta)
end
-- A sample function providing multiple usage cases for various types of user input
local function InputChanged(input, _gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("The mouse has been moved!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("The mouse wheel has been scrolled!")
print("Wheel Movement:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("The left thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("The right thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("The pressure being applied to the left trigger has changed!")
print("Pressure:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("The pressure being applied to the right trigger has changed!")
print("Pressure:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("The user's finger is moving on the screen!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.Gyro then
local _rotInput, rotCFrame = UserInputService:GetDeviceRotation()
local rotX, rotY, rotZ = rotCFrame:toEulerAnglesXYZ()
local rot = Vector3.new(math.deg(rotX), math.deg(rotY), math.deg(rotZ))
print("The rotation of the user's mobile device has been changed!")
print("Position", rotCFrame.p)
print("Rotation:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("The acceleration of the user's mobile device has been changed!")
printMovement(input)
end
end
UserInputService.InputChanged:Connect(InputChanged)

The following example demonstrates one of many usage examples of handling user input from InputEnded depending on its type.

Handling InputEnded

-- In order to use the InputChanged event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key has been released! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button has been released on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)

สรุป

คุณสมบัติ

  • อ่านพร้อมๆ กัน

    เวกเตอร์ 3 ที่อธิบายเดลตาระหว่างการเคลื่อนไหวของเมาส์/จอยสติก

  • อ่านพร้อมๆ กัน

    มี Enum ที่อธิบายประเภทของอินพุตที่ใช้

  • อ่านพร้อมๆ กัน

    อธิบายค่าตำแหน่งของอินพุตนี้

  • อ่านพร้อมๆ กัน

    อธิบายสถานะของการใส่ข้อมูลที่ดำเนินการตามกระบวนการที่เฉพาะเจาะจงขึ้นอยู่กับ UserInputType

  • อ่านพร้อมๆ กัน

    อธิบายประเภทของการป้อนข้อมูลที่ดำเนินการ (เมาส์ แป้นพิมพ์ เกมแพด สัมผัส ฯลฯ)

คุณสมบัติ

Delta

อ่านพร้อมๆ กัน

A Vector3 อธิบายการเปลี่ยนแปลงเดลต้า (การเคลื่อนไหวของเมาส์/จอยสติก)

สิ่งนี้มีประโยชน์เมื่อใช้กับอินพุต position เพื่อติดตามตำแหน่งและการเคลื่อนไหวของเมาส์/จอยสติกของผู้ใช้ เช่น เมื่อคุณกําลังสร้างสคริปต์การเคลื่อนไหวหรือกล้องที่กําหนดเองพิจารณาการติดตามการเปลี่ยนแปลงวัตถุอินพุตโดยใช้เหตุการณ์ Object.Changed หรือเมื่อผู้ใช้เปลี่ยนแปลงการใส่ข้อมูลผ่านเหตุการณ์เช่น UserInputService.InputChanged และ GuiObject.InputChanged

โปรดทราบว่า InputObject ที่ตรงกับ Enum.UserInputType.MouseButton1 (คลิกซ้าย) และ Enum.UserInputType.MouseButton2 (คลิกขวา) ที่จัดหาจากการเรียกคืน InputBegan จะไม่มีการอัปเดต Delta หรือ Position เมื่อสร้างเสร็จ ยกเว้นเมื่อการป้อนของเมาส์สิ้นสุดเพื่อรับ deltas ที่อัปเดตสำหรับการใส่ข้อมูลจากเมาส์ คุณต้องอ้างอิง InputObject จากการโทรกลับ InputChanged หรือเรียก GetMouseDelta()อย่างไรก็ตาม ใดๆ InputObjects ที่ตรงกับการสัมผัสกับอินพุตจะมีการอัปเดตเดลต้าและตำแหน่งในทุกเฟรมตลอดชีวิตของพวกเขา

ดูเพิ่ม:

ตัวอย่างโค้ด

The following example demonstrates one of many usage examples of handling user input from InputChanged depending on its type.

Handling InputChanged

-- In order to use the InputChanged event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- Prints the current input position and the change (delta) in position
local function printMovement(input)
print("Position:", input.Position)
print("Movement Delta:", input.Delta)
end
-- A sample function providing multiple usage cases for various types of user input
local function InputChanged(input, _gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("The mouse has been moved!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("The mouse wheel has been scrolled!")
print("Wheel Movement:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("The left thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("The right thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("The pressure being applied to the left trigger has changed!")
print("Pressure:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("The pressure being applied to the right trigger has changed!")
print("Pressure:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("The user's finger is moving on the screen!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.Gyro then
local _rotInput, rotCFrame = UserInputService:GetDeviceRotation()
local rotX, rotY, rotZ = rotCFrame:toEulerAnglesXYZ()
local rot = Vector3.new(math.deg(rotX), math.deg(rotY), math.deg(rotZ))
print("The rotation of the user's mobile device has been changed!")
print("Position", rotCFrame.p)
print("Rotation:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("The acceleration of the user's mobile device has been changed!")
printMovement(input)
end
end
UserInputService.InputChanged:Connect(InputChanged)

This example creates a binoculars script that decreases the player's FieldOfView() and MouseDeltaSensitivity() when a player with a MouseEnabled() left mouse clicks. The script also points the player's Camera towards the Vector3 world position of the mouse click.

When the player left mouse clicks again, the player's camera reverts back to the a custom Enum.CameraType with the same field of view and CFrame() as before the player zoomed in with the script.

While the player uses the binoculars, the script locks the player's mouse to the center of the screen by setting the player's MouseBehavior() to LockCenter. The player's camera moves when the player moves their mouse according to the InputObject.Delta property passed by InputChanged() indicating the mouse's Vector2 change in screen position.

In order for this example to work as expected, it should be placed in a LocalScript.

Create a Binoculars Script

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- Reset camera back to CFrame and FieldOfView before zoom
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- Allow camera to be changed by script
camera.CameraType = Enum.CameraType.Scriptable
-- Store camera properties before zoom
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- Zoom camera
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- Lock and slow down mouse
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- Reset zoom angles
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- Toggle camera zoom/unzoom
local function MouseClick()
if zoomed then
-- Unzoom camera
ResetCamera()
else
-- Zoom in camera
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- anything higher would make looking up and down harder; recommend anything between 0~1
local smoothness = 0.05 -- recommend anything between 0~1
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end
อ่านพร้อมๆ กัน

มี enum Enum.KeyCode ที่อธิบายถึงชนิดของอินพุตที่ใช้สำหรับประเภทของอินพุตเช่นแป้นพิมพ์จะอธิบายถึงกุญแจที่กดสำหรับอินพุตเช่นเมาส์จะไม่มีข้อมูลเพิ่มเติม

เอนุม


<th>มูลค่า</th>
<th>คําอธิบาย</th>
</tr>
</thead>
<tr>
<td>
<b>ไม่ทราบ</b>
</td>
<td>0</td>
<td />
</tr>
<tr>
<td>
<b>การย้อนกลับ</b>
</td>
<td>8</td>
<td />
</tr>
<tr>
<td>
<b>แท็บ</b>
</td>
<td>9</td>
<td />
</tr>
<tr>
<td>
<b>ล้าง</b>
</td>
<td>12</td>
<td />
</tr>
<tr>
<td>
<b>กลับ</b>
</td>
<td>13</td>
<td />
</tr>
<tr>
<td>
<b>หยุด</b>
</td>
<td>19</td>
<td />
</tr>
<tr>
<td>
<b>หลบหนี</b>
</td>
<td>27</td>
<td />
</tr>
<tr>
<td>
<b>พื้นที่</b>
</td>
<td>32</td>
<td />
</tr>
<tr>
<td>
<b>อ้างถึงสองเท่า</b>
</td>
<td>34</td>
<td />
</tr>
<tr>
<td>
<b>คําอธิบาย</b>
</td>
<td>35</td>
<td />
</tr>
<tr>
<td>
<b>ดอลลาร์</b>
</td>
<td>36</td>
<td />
</tr>
<tr>
<td>
<b>เปอร์เซ็นต์</b>
</td>
<td>37</td>
<td />
</tr>
<tr>
<td>
<b>แอมเปอร์แซนด์</b>
</td>
<td>38</td>
<td />
</tr>
<tr>
<td>
<b>อ้างอิง</b>
</td>
<td>39</td>
<td />
</tr>
<tr>
<td>
<b>เลขานุการซ้าย</b>
</td>
<td>40</td>
<td />
</tr>
<tr>
<td>
<b>RightParenthesis</b>
</td>
<td>41</td>
<td />
</tr>
<tr>
<td>
<b>เครื่องหมายดาว</b>
</td>
<td>42</td>
<td />
</tr>
<tr>
<td>
<b>บวก</b>
</td>
<td>43</td>
<td />
</tr>
<tr>
<td>
<b>คอมมา</b>
</td>
<td>44</td>
<td />
</tr>
<tr>
<td>
<b>ลบ</b>
</td>
<td>45</td>
<td />
</tr>
<tr>
<td>
<b>ระยะเวลา</b>
</td>
<td>46</td>
<td />
</tr>
<tr>
<td>
<b>ฟัน</b>
</td>
<td>47</td>
<td />
</tr>
<tr>
<td>
<b>เป็นศูนย์</b>
</td>
<td>48</td>
<td />
</tr>
<tr>
<td>
<b>หนึ่ง</b>
</td>
<td>49</td>
<td />
</tr>
<tr>
<td>
<b>สอง</b>
</td>
<td>50</td>
<td />
</tr>
<tr>
<td>
<b>สาม</b>
</td>
<td>51</td>
<td />
</tr>
<tr>
<td>
<b>สี่</b>
</td>
<td>52</td>
<td />
</tr>
<tr>
<td>
<b>ห้า</b>
</td>
<td>53</td>
<td />
</tr>
<tr>
<td>
<b>หก</b>
</td>
<td>54</td>
<td />
</tr>
<tr>
<td>
<b>เจ็ด</b>
</td>
<td>55</td>
<td />
</tr>
<tr>
<td>
<b>แปด</b>
</td>
<td>56</td>
<td />
</tr>
<tr>
<td>
<b>เก้า</b>
</td>
<td>57</td>
<td />
</tr>
<tr>
<td>
<b>โคลอน</b>
</td>
<td>58</td>
<td />
</tr>
<tr>
<td>
<b>เซมิโคลอน</b>
</td>
<td>59</td>
<td />
</tr>
<tr>
<td>
<b>น้อยกว่า</b>
</td>
<td>60</td>
<td />
</tr>
<tr>
<td>
<b>เท่ากับ</b>
</td>
<td>61</td>
<td />
</tr>
<tr>
<td>
<b>มากกว่า</b>
</td>
<td>62</td>
<td />
</tr>
<tr>
<td>
<b>คำถาม</b>
</td>
<td>63</td>
<td />
</tr>
<tr>
<td>
<b>At</b>
</td>
<td>64</td>
<td />
</tr>
<tr>
<td>
<b>เครื่องหมายซ้าย</b>
</td>
<td>91</td>
<td />
</tr>
<tr>
<td>
<b>แบ็คสแลช</b>
</td>
<td>92</td>
<td />
</tr>
<tr>
<td>
<b>กรอบด้านขวา</b>
</td>
<td>93</td>
<td />
</tr>
<tr>
<td>
<b>การดูแล</b>
</td>
<td>94</td>
<td />
</tr>
<tr>
<td>
<b>เครื่องหมายขีดเส้นใต้</b>
</td>
<td>95</td>
<td />
</tr>
<tr>
<td>
<b>อ้างอิง</b>
</td>
<td>96</td>
<td />
</tr>
<tr>
<td>
<b>A</b>
</td>
<td>97</td>
<td />
</tr>
<tr>
<td>
<b>B</b>
</td>
<td>98</td>
<td />
</tr>
<tr>
<td>
<b>C</b>
</td>
<td>99</td>
<td />
</tr>
<tr>
<td>
<b>D</b>
</td>
<td>100</td>
<td />
</tr>
<tr>
<td>
<b>E</b>
</td>
<td>101</td>
<td />
</tr>
<tr>
<td>
<b>F</b>
</td>
<td>102</td>
<td />
</tr>
<tr>
<td>
<b>G</b>
</td>
<td>103</td>
<td />
</tr>
<tr>
<td>
<b>H</b>
</td>
<td>104</td>
<td />
</tr>
<tr>
<td>
<b>I</b>
</td>
<td>105</td>
<td />
</tr>
<tr>
<td>
<b>J</b>
</td>
<td>106</td>
<td />
</tr>
<tr>
<td>
<b>K</b>
</td>
<td>107</td>
<td />
</tr>
<tr>
<td>
<b>L</b>
</td>
<td>108</td>
<td />
</tr>
<tr>
<td>
<b>M</b>
</td>
<td>109</td>
<td />
</tr>
<tr>
<td>
<b>N</b>
</td>
<td>110</td>
<td />
</tr>
<tr>
<td>
<b>O</b>
</td>
<td>111</td>
<td />
</tr>
<tr>
<td>
<b>P</b>
</td>
<td>112</td>
<td />
</tr>
<tr>
<td>
<b>Q</b>
</td>
<td>113</td>
<td />
</tr>
<tr>
<td>
<b>R</b>
</td>
<td>114</td>
<td />
</tr>
<tr>
<td>
<b>S</b>
</td>
<td>115</td>
<td />
</tr>
<tr>
<td>
<b>T</b>
</td>
<td>116</td>
<td />
</tr>
<tr>
<td>
<b>U</b>
</td>
<td>117</td>
<td />
</tr>
<tr>
<td>
<b>V</b>
</td>
<td>118</td>
<td />
</tr>
<tr>
<td>
<b>W</b>
</td>
<td>119</td>
<td />
</tr>
<tr>
<td>
<b>X</b>
</td>
<td>120</td>
<td />
</tr>
<tr>
<td>
<b>Y</b>
</td>
<td>121</td>
<td />
</tr>
<tr>
<td>
<b>Z</b>
</td>
<td>122</td>
<td />
</tr>
<tr>
<td>
<b>เคิร์ลซ้าย</b>
</td>
<td>123</td>
<td />
</tr>
<tr>
<td>
<b>ท่อ</b>
</td>
<td>124</td>
<td />
</tr>
<tr>
<td>
<b>ขวาโค้ง</b>
</td>
<td>125</td>
<td />
</tr>
<tr>
<td>
<b>เครื่องหมายบวก</b>
</td>
<td>126</td>
<td />
</tr>
<tr>
<td>
<b>ลบ</b>
</td>
<td>127</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์เป็นศูนย์</b>
</td>
<td>256</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์หนึ่ง</b>
</td>
<td>257</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์สอง</b>
</td>
<td>258</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์สาม</b>
</td>
<td>259</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์สี่</b>
</td>
<td>260</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์ห้า</b>
</td>
<td>261</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์หก</b>
</td>
<td>262</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์เจ็ด</b>
</td>
<td>263</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์แปด</b>
</td>
<td>264</td>
<td />
</tr>
<tr>
<td>
<b>แป้นคีย์เก้า</b>
</td>
<td>265</td>
<td />
</tr>
<tr>
<td>
<b>ระยะเวลาแป้นพิมพ์</b>
</td>
<td>266</td>
<td />
</tr>
<tr>
<td>
<b>แยกแป้นพิมพ์</b>
</td>
<td>267</td>
<td />
</tr>
<tr>
<td>
<b>แป้นคีย์หลาย</b>
</td>
<td>268</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์ลบ</b>
</td>
<td>269</td>
<td />
</tr>
<tr>
<td>
<b>แป้นพิมพ์เพิ่ม</b>
</td>
<td>270</td>
<td />
</tr>
<tr>
<td>
<b>กุญแจเข้า</b>
</td>
<td>271</td>
<td />
</tr>
<tr>
<td>
<b>แป้นคีย์เท่ากับ</b>
</td>
<td>272</td>
<td />
</tr>
<tr>
<td>
<b>Up</b>
</td>
<td>273</td>
<td />
</tr>
<tr>
<td>
<b>ลง</b>
</td>
<td>274</td>
<td />
</tr>
<tr>
<td>
<b>ขวา</b>
</td>
<td>275</td>
<td />
</tr>
<tr>
<td>
<b>ซ้าย</b>
</td>
<td>276</td>
<td />
</tr>
<tr>
<td>
<b>สอด</b>
</td>
<td>277</td>
<td />
</tr>
<tr>
<td>
<b>บ้าน</b>
</td>
<td>278</td>
<td />
</tr>
<tr>
<td>
<b>สิ้นสุด</b>
</td>
<td>279</td>
<td />
</tr>
<tr>
<td>
<b>หน้าขึ้น</b>
</td>
<td>280</td>
<td />
</tr>
<tr>
<td>
<b>หน้าลง</b>
</td>
<td>281</td>
<td />
</tr>
<tr>
<td>
<b>Shift ซ้าย</b>
</td>
<td>304</td>
<td />
</tr>
<tr>
<td>
<b>Shift ขวา</b>
</td>
<td>303</td>
<td />
</tr>
<tr>
<td>
<b>Meta ซ้าย</b>
</td>
<td>310</td>
<td />
</tr>
<tr>
<td>
<b>Meta ขวา</b>
</td>
<td>309</td>
<td />
</tr>
<tr>
<td>
<b>Alt ซ้าย</b>
</td>
<td>308</td>
<td />
</tr>
<tr>
<td>
<b>RightAlt</b>
</td>
<td>307</td>
<td />
</tr>
<tr>
<td>
<b>ควบคุมซ้าย</b>
</td>
<td>306</td>
<td />
</tr>
<tr>
<td>
<b>การควบคุมขวา</b>
</td>
<td>305</td>
<td />
</tr>
<tr>
<td>
<b>แคปส์ล็อก</b>
</td>
<td>301</td>
<td />
</tr>
<tr>
<td>
<b>ล็อคตัวเลข</b>
</td>
<td>300</td>
<td />
</tr>
<tr>
<td>
<b>ล็อคการเลื่อน</b>
</td>
<td>302</td>
<td />
</tr>
<tr>
<td>
<b>ซ้ายซุปเปอร์</b>
</td>
<td>311</td>
<td />
</tr>
<tr>
<td>
<b>ซุปเปอร์ขวา</b>
</td>
<td>312</td>
<td />
</tr>
<tr>
<td>
<b>โหมด</b>
</td>
<td>313</td>
<td />
</tr>
<tr>
<td>
<b>รวบรวม</b>
</td>
<td>314</td>
<td />
</tr>
<tr>
<td>
<b>ช่วยเหลือ</b>
</td>
<td>315</td>
<td />
</tr>
<tr>
<td>
<b>พิมพ์</b>
</td>
<td>316</td>
<td />
</tr>
<tr>
<td>
<b>SysReq</b>
</td>
<td>317</td>
<td />
</tr>
<tr>
<td>
<b>ทำลาย</b>
</td>
<td>318</td>
<td />
</tr>
<tr>
<td>
<b>เมนู</b>
</td>
<td>319</td>
<td />
</tr>
<tr>
<td>
<b>พลังงาน</b>
</td>
<td>320</td>
<td />
</tr>
<tr>
<td>
<b>ยูโร</b>
</td>
<td>321</td>
<td />
</tr>
<tr>
<td>
<b>ยกเลิก</b>
</td>
<td>322</td>
<td />
</tr>
<tr>
<td>
<b>F1</b>
</td>
<td>282</td>
<td />
</tr>
<tr>
<td>
<b>F2</b>
</td>
<td>283</td>
<td />
</tr>
<tr>
<td>
<b>F3</b>
</td>
<td>284</td>
<td />
</tr>
<tr>
<td>
<b>F4</b>
</td>
<td>285</td>
<td />
</tr>
<tr>
<td>
<b>F5</b>
</td>
<td>286</td>
<td />
</tr>
<tr>
<td>
<b>F6</b>
</td>
<td>287</td>
<td />
</tr>
<tr>
<td>
<b>F7</b>
</td>
<td>288</td>
<td />
</tr>
<tr>
<td>
<b>F8</b>
</td>
<td>289</td>
<td />
</tr>
<tr>
<td>
<b>F9</b>
</td>
<td>290</td>
<td />
</tr>
<tr>
<td>
<b>F10</b>
</td>
<td>291</td>
<td />
</tr>
<tr>
<td>
<b>F11</b>
</td>
<td>292</td>
<td />
</tr>
<tr>
<td>
<b>F12</b>
</td>
<td>293</td>
<td />
</tr>
<tr>
<td>
<b>F13</b>
</td>
<td>294</td>
<td />
</tr>
<tr>
<td>
<b>F14</b>
</td>
<td>295</td>
<td />
</tr>
<tr>
<td>
<b>F15</b>
</td>
<td>296</td>
<td />
</tr>
<tr>
<td>
<b>โลก 0</b>
</td>
<td>160</td>
<td />
</tr>
<tr>
<td>
<b>โลก 1</b>
</td>
<td>161</td>
<td />
</tr>
<tr>
<td>
<b>โลก 2</b>
</td>
<td>162</td>
<td />
</tr>
<tr>
<td>
<b>โลก 3</b>
</td>
<td>163</td>
<td />
</tr>
<tr>
<td>
<b>โลก 4</b>
</td>
<td>164</td>
<td />
</tr>
<tr>
<td>
<b>โลก 5</b>
</td>
<td>165</td>
<td />
</tr>
<tr>
<td>
<b>โลก 6</b>
</td>
<td>166</td>
<td />
</tr>
<tr>
<td>
<b>World7</b>
</td>
<td>167</td>
<td />
</tr>
<tr>
<td>
<b>โลก 8</b>
</td>
<td>168</td>
<td />
</tr>
<tr>
<td>
<b>โลก 9</b>
</td>
<td>169</td>
<td />
</tr>
<tr>
<td>
<b>โลก 10</b>
</td>
<td>170</td>
<td />
</tr>
<tr>
<td>
<b>โลก 11</b>
</td>
<td>171</td>
<td />
</tr>
<tr>
<td>
<b>โลก 12</b>
</td>
<td>172</td>
<td />
</tr>
<tr>
<td>
<b>โลก 13</b>
</td>
<td>173</td>
<td />
</tr>
<tr>
<td>
<b>โลก 14</b>
</td>
<td>174</td>
<td />
</tr>
<tr>
<td>
<b>โลก 15</b>
</td>
<td>175</td>
<td />
</tr>
<tr>
<td>
<b>โลก 16</b>
</td>
<td>176</td>
<td />
</tr>
<tr>
<td>
<b>โลก 17</b>
</td>
<td>177</td>
<td />
</tr>
<tr>
<td>
<b>โลก 18</b>
</td>
<td>178</td>
<td />
</tr>
<tr>
<td>
<b>โลก 19</b>
</td>
<td>179</td>
<td />
</tr>
<tr>
<td>
<b>โลก 20</b>
</td>
<td>180</td>
<td />
</tr>
<tr>
<td>
<b>โลก 21</b>
</td>
<td>181</td>
<td />
</tr>
<tr>
<td>
<b>โลก 22</b>
</td>
<td>182</td>
<td />
</tr>
<tr>
<td>
<b>โลก 23</b>
</td>
<td>183</td>
<td />
</tr>
<tr>
<td>
<b>โลก 24</b>
</td>
<td>184</td>
<td />
</tr>
<tr>
<td>
<b>โลก 25</b>
</td>
<td>185</td>
<td />
</tr>
<tr>
<td>
<b>โลก 26</b>
</td>
<td>186</td>
<td />
</tr>
<tr>
<td>
<b>โลก 27</b>
</td>
<td>187</td>
<td />
</tr>
<tr>
<td>
<b>โลก 28</b>
</td>
<td>188</td>
<td />
</tr>
<tr>
<td>
<b>โลก 29</b>
</td>
<td>189</td>
<td />
</tr>
<tr>
<td>
<b>โลก 30</b>
</td>
<td>190</td>
<td />
</tr>
<tr>
<td>
<b>โลก 31</b>
</td>
<td>191</td>
<td />
</tr>
<tr>
<td>
<b>โลก32</b>
</td>
<td>192</td>
<td />
</tr>
<tr>
<td>
<b>โลก33</b>
</td>
<td>193</td>
<td />
</tr>
<tr>
<td>
<b>World34</b>
</td>
<td>194</td>
<td />
</tr>
<tr>
<td>
<b>โลก35</b>
</td>
<td>195</td>
<td />
</tr>
<tr>
<td>
<b>โลก36</b>
</td>
<td>196</td>
<td />
</tr>
<tr>
<td>
<b>World37</b>
</td>
<td>197</td>
<td />
</tr>
<tr>
<td>
<b>โลก38</b>
</td>
<td>198</td>
<td />
</tr>
<tr>
<td>
<b>World39</b>
</td>
<td>199</td>
<td />
</tr>
<tr>
<td>
<b>โลก 40</b>
</td>
<td>200</td>
<td />
</tr>
<tr>
<td>
<b>World41</b>
</td>
<td>201</td>
<td />
</tr>
<tr>
<td>
<b>โลก 42</b>
</td>
<td>202</td>
<td />
</tr>
<tr>
<td>
<b>World43</b>
</td>
<td>203</td>
<td />
</tr>
<tr>
<td>
<b>World44</b>
</td>
<td>204</td>
<td />
</tr>
<tr>
<td>
<b>โลก 45</b>
</td>
<td>205</td>
<td />
</tr>
<tr>
<td>
<b>โลก 46</b>
</td>
<td>206</td>
<td />
</tr>
<tr>
<td>
<b>World47</b>
</td>
<td>207</td>
<td />
</tr>
<tr>
<td>
<b>โลก 48</b>
</td>
<td>208</td>
<td />
</tr>
<tr>
<td>
<b>โลก 49</b>
</td>
<td>209</td>
<td />
</tr>
<tr>
<td>
<b>โลก 50</b>
</td>
<td>210</td>
<td />
</tr>
<tr>
<td>
<b>โลก 51</b>
</td>
<td>211</td>
<td />
</tr>
<tr>
<td>
<b>โลก52</b>
</td>
<td>212</td>
<td />
</tr>
<tr>
<td>
<b>โลก53</b>
</td>
<td>213</td>
<td />
</tr>
<tr>
<td>
<b>โลก54</b>
</td>
<td>214</td>
<td />
</tr>
<tr>
<td>
<b>โลก55</b>
</td>
<td>215</td>
<td />
</tr>
<tr>
<td>
<b>โลก56</b>
</td>
<td>216</td>
<td />
</tr>
<tr>
<td>
<b>โลก57</b>
</td>
<td>217</td>
<td />
</tr>
<tr>
<td>
<b>โลก58</b>
</td>
<td>218</td>
<td />
</tr>
<tr>
<td>
<b>โลก59</b>
</td>
<td>219</td>
<td />
</tr>
<tr>
<td>
<b>World60</b>
</td>
<td>220</td>
<td />
</tr>
<tr>
<td>
<b>โลก 61</b>
</td>
<td>221</td>
<td />
</tr>
<tr>
<td>
<b>โลก 62</b>
</td>
<td>222</td>
<td />
</tr>
<tr>
<td>
<b>World63</b>
</td>
<td>223</td>
<td />
</tr>
<tr>
<td>
<b>โลก 64</b>
</td>
<td>224</td>
<td />
</tr>
<tr>
<td>
<b>โลก65</b>
</td>
<td>225</td>
<td />
</tr>
<tr>
<td>
<b>โลก66</b>
</td>
<td>226</td>
<td />
</tr>
<tr>
<td>
<b>โลก67</b>
</td>
<td>227</td>
<td />
</tr>
<tr>
<td>
<b>โลก 68</b>
</td>
<td>228</td>
<td />
</tr>
<tr>
<td>
<b>โลก 69</b>
</td>
<td>229</td>
<td />
</tr>
<tr>
<td>
<b>World70</b>
</td>
<td>230</td>
<td />
</tr>
<tr>
<td>
<b>โลก 71</b>
</td>
<td>231</td>
<td />
</tr>
<tr>
<td>
<b>World72</b>
</td>
<td>232</td>
<td />
</tr>
<tr>
<td>
<b>โลก 73</b>
</td>
<td>233</td>
<td />
</tr>
<tr>
<td>
<b>World74</b>
</td>
<td>234</td>
<td />
</tr>
<tr>
<td>
<b>World75</b>
</td>
<td>235</td>
<td />
</tr>
<tr>
<td>
<b>World76</b>
</td>
<td>236</td>
<td />
</tr>
<tr>
<td>
<b>โลก 77</b>
</td>
<td>237</td>
<td />
</tr>
<tr>
<td>
<b>World78</b>
</td>
<td>238</td>
<td />
</tr>
<tr>
<td>
<b>World79</b>
</td>
<td>239</td>
<td />
</tr>
<tr>
<td>
<b>โลก 80</b>
</td>
<td>240</td>
<td />
</tr>
<tr>
<td>
<b>โลก 81</b>
</td>
<td>241</td>
<td />
</tr>
<tr>
<td>
<b>โลก 82</b>
</td>
<td>242</td>
<td />
</tr>
<tr>
<td>
<b>โลก83</b>
</td>
<td>243</td>
<td />
</tr>
<tr>
<td>
<b>โลก 84</b>
</td>
<td>244</td>
<td />
</tr>
<tr>
<td>
<b>โลก85</b>
</td>
<td>245</td>
<td />
</tr>
<tr>
<td>
<b>โลก86</b>
</td>
<td>246</td>
<td />
</tr>
<tr>
<td>
<b>โลก87</b>
</td>
<td>247</td>
<td />
</tr>
<tr>
<td>
<b>โลก88</b>
</td>
<td>248</td>
<td />
</tr>
<tr>
<td>
<b>World89</b>
</td>
<td>249</td>
<td />
</tr>
<tr>
<td>
<b>โลก 90</b>
</td>
<td>250</td>
<td />
</tr>
<tr>
<td>
<b>โลก 91</b>
</td>
<td>251</td>
<td />
</tr>
<tr>
<td>
<b>โลก 92</b>
</td>
<td>252</td>
<td />
</tr>
<tr>
<td>
<b>โลก 93</b>
</td>
<td>253</td>
<td />
</tr>
<tr>
<td>
<b>โลก 94</b>
</td>
<td>254</td>
<td />
</tr>
<tr>
<td>
<b>โลก 95</b>
</td>
<td>255</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม X</b>
</td>
<td>1000</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม Y</b>
</td>
<td>1001</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่มA</b>
</td>
<td>1002</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่มB</b>
</td>
<td>1003</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม R1</b>
</td>
<td>1004</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม L1</b>
</td>
<td>1005</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม R2</b>
</td>
<td>1006</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม L2</b>
</td>
<td>1007</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม R3</b>
</td>
<td>1008</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่ม L3</b>
</td>
<td>1009</td>
<td />
</tr>
<tr>
<td>
<b>ปุ่มเริ่มต้น</b>
</td>
<td>1010</td>
<td />
</tr>
<tr>
<td>
<b>เลือกปุ่ม</b>
</td>
<td>1011</td>
<td />
</tr>
<tr>
<td>
<b>DPadซ้าย</b>
</td>
<td>1012</td>
<td />
</tr>
<tr>
<td>
<b>DPadขวา</b>
</td>
<td>1013</td>
<td />
</tr>
<tr>
<td>
<b>DPadขึ้น</b>
</td>
<td>1014</td>
<td />
</tr>
<tr>
<td>
<b>DPadลง</b>
</td>
<td>1015</td>
<td />
</tr>
<tr>
<td>
<b>สติกเท้า1</b>
</td>
<td>1016</td>
<td />
</tr>
<tr>
<td>
<b>สติกเท้า2</b>
</td>
<td>1017</td>
</tr>
ชื่อ

ดูเพิ่ม:

ตัวอย่างโค้ด

This example gets a list of navigation gamepads and a list of their supported Enum.KeyCodes. Then, it iterates through the supported KeyCode list and binds the ButtonX and X keys to functions if they are supported by a gamepad using the ContextActionService.

Binding Supported Gamepad KeyCodes

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local function actionHandler(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Action Handler: " .. actionName)
print(inputObject)
end
-- Since this function does not return anything, this handler will
-- "sink" the input and no other action handlers will be called after
-- this one.
end
local navGamepads = UserInputService:GetNavigationGamepads()
for _, gamepad in pairs(navGamepads) do
local supportedKeyCodes = UserInputService:GetSupportedGamepadKeyCodes(gamepad)
for _, keycode in pairs(supportedKeyCodes) do
if keycode == Enum.KeyCode.ButtonX then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.ButtonX)
end
if keycode == Enum.KeyCode.X then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.X)
end
end
end

Position

อ่านพร้อมๆ กัน

คุณสมบัตินี้อธิบายถึงมูลค่าตําแหน่งของอินพุตนี้ Vector3

สำหรับการใส่ข้อมูลด้วยเมาส์และแตะ นี่คือตำแหน่งหน้าจอของเมาส์/แตะที่อธิบายไว้ในส่วนประกอบ X และ Yการใส่ที่ใช้กับองค์ประกอบ GUI (เช่นจากแถบด้านบน) ถูกนับรวมในตำแหน่ง

สำหรับการใส่ล้อเลื่อน ส่วนประกอบ Z จะอธิบายว่าล้อถูกย้ายไปข้างหน้า (1) ย้อนกลับ (-1) หรือไม่มีเลย (0)

สำหรับการใส่ Enum.KeyCode นี้บ่งบอกถึงตำแหน่งของ Mouse ผู้เล่น

โปรดทราบว่า InputObject ที่ตรงกับ Enum.UserInputType.MouseButton1 (คลิกซ้าย) และ Enum.UserInputType.MouseButton2 (คลิกขวา) ที่จัดหาจากการเรียกคืน InputBegan จะไม่มีการอัปเดต Delta หรือ Position เมื่อสร้างเสร็จ ยกเว้นเมื่อการป้อนของเมาส์สิ้นสุดเพื่อรับตำแหน่งที่อัปเดตสำหรับการใส่ข้อมูลจากเมาส์ คุณต้องอ้างอิง InputObject จากการโทรกลับ InputChanged หรือเรียก GetMouseLocation()อย่างไรก็ตาม ใดๆ InputObjects ที่ตรงกับการสัมผัสกับอินพุตจะมีการอัปเดตเดลต้าและตำแหน่งในทุกเฟรมตลอดชีวิตของพวกเขา

ดูเพิ่มเติม

ตัวอย่างโค้ด

The following example demonstrates one of many usage examples of handling user input from InputBegan depending on its type.

Handling InputBegan

-- In order to use the InputBegan event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)

The following example demonstrates one of many usage examples of handling user input from InputChanged depending on its type.

Handling InputChanged

-- In order to use the InputChanged event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- Prints the current input position and the change (delta) in position
local function printMovement(input)
print("Position:", input.Position)
print("Movement Delta:", input.Delta)
end
-- A sample function providing multiple usage cases for various types of user input
local function InputChanged(input, _gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("The mouse has been moved!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("The mouse wheel has been scrolled!")
print("Wheel Movement:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("The left thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("The right thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("The pressure being applied to the left trigger has changed!")
print("Pressure:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("The pressure being applied to the right trigger has changed!")
print("Pressure:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("The user's finger is moving on the screen!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.Gyro then
local _rotInput, rotCFrame = UserInputService:GetDeviceRotation()
local rotX, rotY, rotZ = rotCFrame:toEulerAnglesXYZ()
local rot = Vector3.new(math.deg(rotX), math.deg(rotY), math.deg(rotZ))
print("The rotation of the user's mobile device has been changed!")
print("Position", rotCFrame.p)
print("Rotation:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("The acceleration of the user's mobile device has been changed!")
printMovement(input)
end
end
UserInputService.InputChanged:Connect(InputChanged)

The following example demonstrates one of many usage examples of handling user input from InputEnded depending on its type.

Handling InputEnded

-- In order to use the InputChanged event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key has been released! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button has been released on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)

UserInputState

อ่านพร้อมๆ กัน

UserInputState อธิบายสถานะของการใส่ข้อมูลที่ดำเนินการตามกระบวนการที่เฉพาะเจาะจงขึ้นอยู่กับ UserInputTypeมันใช้ enum ของชื่อเดียวกัน, Enum.UserInputState .ดูหน้ารายการสําหรับรายการของค่าทั้งหมดที่เป็นไปได้สําหรับคุณสมบัตินี้บนหน้ารายการ

ดูเพิ่ม:

ตัวอย่างโค้ด

This example gets a list of navigation gamepads and a list of their supported Enum.KeyCodes. Then, it iterates through the supported KeyCode list and binds the ButtonX and X keys to functions if they are supported by a gamepad using the ContextActionService.

Binding Supported Gamepad KeyCodes

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local function actionHandler(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Action Handler: " .. actionName)
print(inputObject)
end
-- Since this function does not return anything, this handler will
-- "sink" the input and no other action handlers will be called after
-- this one.
end
local navGamepads = UserInputService:GetNavigationGamepads()
for _, gamepad in pairs(navGamepads) do
local supportedKeyCodes = UserInputService:GetSupportedGamepadKeyCodes(gamepad)
for _, keycode in pairs(supportedKeyCodes) do
if keycode == Enum.KeyCode.ButtonX then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.ButtonX)
end
if keycode == Enum.KeyCode.X then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.X)
end
end
end

By default, Roblox relies on a LocalScript to control the user's camera. However, this script can be overridden with a custom CameraScript. The example below demonstrates how to create a custom script to control the user's camera using many of the UserInputService events.

The script is broken into two parts:

  1. Mobile camera events, which rely on touch events
  2. Non-mobile camera events, which rely on keyboard input and tracking the user's movement

First, the camera script needs utility functions to setup the camera and set its Camera.CameraType to Scriptable so that the script can control the camera. It also needs a function to update the camera when it moves, rotates, and zooms.

Using touch events allows us to track user input as they interact with the touchscreen on their mobile device. These events allow us to handle camera movement, rotation, and zoom.

The second half of the code sample adds camera support for players on desktop devices. When input begans, the function Input() checks that the state of the input is Enum.UserInputState.Begin to ignore all keypress inputs other than when the user first presses a key down. When the user presses I and O the camera zooms in and out. When the presses down and moves their left mouse button, the script locks the player's mouse by changing the UserInputService.MouseBehavior property. The camera rotates according to the mouse's change in screen position. When the player moves their character, the camera moves with them.

All of the parts discussed above are combined and shown in the code sample below.

Create a Custom CameraScript

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local torso = character:WaitForChild("HumanoidRootPart")
local playerPosition = torso.Position
local default_CameraPosition = torso.Position
local default_CameraRotation = Vector2.new(0, math.rad(-60))
local default_CameraZoom = 15
local cameraPosition = default_CameraPosition
local cameraRotation = default_CameraRotation
local cameraZoom = default_CameraZoom
local cameraZoomBounds = nil -- {10,200}
local cameraRotateSpeed = 10
local cameraMouseRotateSpeed = 0.25
local cameraTouchRotateSpeed = 10
local function SetCameraMode()
camera.CameraType = "Scriptable"
camera.FieldOfView = 80
camera.CameraSubject = nil
end
local function UpdateCamera()
SetCameraMode()
local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0) * CFrame.Angles(cameraRotation.Y, 0, 0)
camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame * Vector3.new(0, 0, cameraZoom)
camera.Focus = camera.CFrame - Vector3.new(0, camera.CFrame.p.Y, 0)
end
local lastTouchTranslation = nil
local function TouchMove(_touchPositions, totalTranslation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = totalTranslation - lastTouchTranslation
cameraPosition = cameraPosition + Vector3.new(difference.X, 0, difference.Y)
UpdateCamera()
end
lastTouchTranslation = totalTranslation
end
local lastTouchRotation = nil
local function TouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = rotation - lastTouchRotation
cameraRotation = cameraRotation
+ Vector2.new(-difference, 0) * math.rad(cameraTouchRotateSpeed * cameraRotateSpeed)
UpdateCamera()
end
lastTouchRotation = rotation
end
local lastTouchScale = nil
local function TouchZoom(_touchPositions, scale, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = scale - lastTouchScale
cameraZoom = cameraZoom * (1 + difference)
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
lastTouchScale = scale
end
local function Input(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
if inputObject.UserInputState == Enum.UserInputState.Begin then
-- (I) Zoom In
if inputObject.KeyCode == Enum.KeyCode.I then
cameraZoom = cameraZoom - 15
elseif inputObject.KeyCode == Enum.KeyCode.O then
cameraZoom = cameraZoom + 15
end
-- (O) Zoom Out
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
end
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
if pressed then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
local rotation = UserInputService:GetMouseDelta()
cameraRotation = cameraRotation + rotation * math.rad(cameraMouseRotateSpeed)
else
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end
local function PlayerChanged()
local movement = torso.Position - playerPosition
cameraPosition = cameraPosition + movement
playerPosition = torso.Position
UpdateCamera()
end
-- Determine whether the user is on a mobile device
if UserInputService.TouchEnabled then
-- The user is on a mobile device, use Touch events
UserInputService.TouchPan:Connect(TouchMove)
UserInputService.TouchRotate:Connect(TouchRotate)
UserInputService.TouchPinch:Connect(TouchZoom)
else
-- The user is not on a mobile device use Input events
UserInputService.InputBegan:Connect(Input)
UserInputService.InputChanged:Connect(Input)
UserInputService.InputEnded:Connect(Input)
-- Camera controlled by player movement
task.wait(2)
RunService:BindToRenderStep("PlayerChanged", Enum.RenderPriority.Camera.Value - 1, PlayerChanged)
end

UserInputType

อ่านพร้อมๆ กัน

ประเภทการใส่ข้อมูลผู้ใช้ เป็นคุณสมบัติที่อธิบายประเภทของการใส่ข้อมูลที่ InputObject นี้แทนที่ เช่น การใส่เมาส์ แป้นพิมพ์ การสัมผัส หรือการใส่เกมแพดมันใช้ enum ของชื่อเดียวกัน, Enum.UserInputType .ดูหน้ารายการสําหรับรายการของค่าทั้งหมดที่เป็นไปได้สําหรับคุณสมบัตินี้บนหน้ารายการ

ดูเพิ่ม:

ตัวอย่างโค้ด

The following example demonstrates one of many usage examples of handling user input from InputChanged depending on its type.

Handling InputChanged

-- In order to use the InputChanged event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- Prints the current input position and the change (delta) in position
local function printMovement(input)
print("Position:", input.Position)
print("Movement Delta:", input.Delta)
end
-- A sample function providing multiple usage cases for various types of user input
local function InputChanged(input, _gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("The mouse has been moved!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("The mouse wheel has been scrolled!")
print("Wheel Movement:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("The left thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("The right thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("The pressure being applied to the left trigger has changed!")
print("Pressure:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("The pressure being applied to the right trigger has changed!")
print("Pressure:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("The user's finger is moving on the screen!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.Gyro then
local _rotInput, rotCFrame = UserInputService:GetDeviceRotation()
local rotX, rotY, rotZ = rotCFrame:toEulerAnglesXYZ()
local rot = Vector3.new(math.deg(rotX), math.deg(rotY), math.deg(rotZ))
print("The rotation of the user's mobile device has been changed!")
print("Position", rotCFrame.p)
print("Rotation:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("The acceleration of the user's mobile device has been changed!")
printMovement(input)
end
end
UserInputService.InputChanged:Connect(InputChanged)

The following example demonstrates one of many usage examples of handling user input from InputBegan depending on its type.

Handling InputBegan

-- In order to use the InputBegan event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)

By default, Roblox relies on a LocalScript to control the user's camera. However, this script can be overridden with a custom CameraScript. The example below demonstrates how to create a custom script to control the user's camera using many of the UserInputService events.

The script is broken into two parts:

  1. Mobile camera events, which rely on touch events
  2. Non-mobile camera events, which rely on keyboard input and tracking the user's movement

First, the camera script needs utility functions to setup the camera and set its Camera.CameraType to Scriptable so that the script can control the camera. It also needs a function to update the camera when it moves, rotates, and zooms.

Using touch events allows us to track user input as they interact with the touchscreen on their mobile device. These events allow us to handle camera movement, rotation, and zoom.

The second half of the code sample adds camera support for players on desktop devices. When input begans, the function Input() checks that the state of the input is Enum.UserInputState.Begin to ignore all keypress inputs other than when the user first presses a key down. When the user presses I and O the camera zooms in and out. When the presses down and moves their left mouse button, the script locks the player's mouse by changing the UserInputService.MouseBehavior property. The camera rotates according to the mouse's change in screen position. When the player moves their character, the camera moves with them.

All of the parts discussed above are combined and shown in the code sample below.

Create a Custom CameraScript

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local torso = character:WaitForChild("HumanoidRootPart")
local playerPosition = torso.Position
local default_CameraPosition = torso.Position
local default_CameraRotation = Vector2.new(0, math.rad(-60))
local default_CameraZoom = 15
local cameraPosition = default_CameraPosition
local cameraRotation = default_CameraRotation
local cameraZoom = default_CameraZoom
local cameraZoomBounds = nil -- {10,200}
local cameraRotateSpeed = 10
local cameraMouseRotateSpeed = 0.25
local cameraTouchRotateSpeed = 10
local function SetCameraMode()
camera.CameraType = "Scriptable"
camera.FieldOfView = 80
camera.CameraSubject = nil
end
local function UpdateCamera()
SetCameraMode()
local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0) * CFrame.Angles(cameraRotation.Y, 0, 0)
camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame * Vector3.new(0, 0, cameraZoom)
camera.Focus = camera.CFrame - Vector3.new(0, camera.CFrame.p.Y, 0)
end
local lastTouchTranslation = nil
local function TouchMove(_touchPositions, totalTranslation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = totalTranslation - lastTouchTranslation
cameraPosition = cameraPosition + Vector3.new(difference.X, 0, difference.Y)
UpdateCamera()
end
lastTouchTranslation = totalTranslation
end
local lastTouchRotation = nil
local function TouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = rotation - lastTouchRotation
cameraRotation = cameraRotation
+ Vector2.new(-difference, 0) * math.rad(cameraTouchRotateSpeed * cameraRotateSpeed)
UpdateCamera()
end
lastTouchRotation = rotation
end
local lastTouchScale = nil
local function TouchZoom(_touchPositions, scale, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = scale - lastTouchScale
cameraZoom = cameraZoom * (1 + difference)
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
lastTouchScale = scale
end
local function Input(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
if inputObject.UserInputState == Enum.UserInputState.Begin then
-- (I) Zoom In
if inputObject.KeyCode == Enum.KeyCode.I then
cameraZoom = cameraZoom - 15
elseif inputObject.KeyCode == Enum.KeyCode.O then
cameraZoom = cameraZoom + 15
end
-- (O) Zoom Out
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
end
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
if pressed then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
local rotation = UserInputService:GetMouseDelta()
cameraRotation = cameraRotation + rotation * math.rad(cameraMouseRotateSpeed)
else
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end
local function PlayerChanged()
local movement = torso.Position - playerPosition
cameraPosition = cameraPosition + movement
playerPosition = torso.Position
UpdateCamera()
end
-- Determine whether the user is on a mobile device
if UserInputService.TouchEnabled then
-- The user is on a mobile device, use Touch events
UserInputService.TouchPan:Connect(TouchMove)
UserInputService.TouchRotate:Connect(TouchRotate)
UserInputService.TouchPinch:Connect(TouchZoom)
else
-- The user is not on a mobile device use Input events
UserInputService.InputBegan:Connect(Input)
UserInputService.InputChanged:Connect(Input)
UserInputService.InputEnded:Connect(Input)
-- Camera controlled by player movement
task.wait(2)
RunService:BindToRenderStep("PlayerChanged", Enum.RenderPriority.Camera.Value - 1, PlayerChanged)
end

วิธีการ

IsModifierKeyDown

พารามิเตอร์

modifierKey: Enum.ModifierKey
ค่าเริ่มต้น: ""

ส่งค่ากลับ

อีเวนต์