View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vídeos

Abrir menu Fechar menu
  • Coleções
  • Todos os vídeos
  • Sobre

Mais vídeos

  • Sobre
  • Código
  • Bring keyboard and mouse gaming to iPad

    Level up your iPad games and add in keyboard, mouse, and trackpad controls. Discover how to use the Game Controller framework to augment your existing titles, bring over games from other platforms, or dream up entirely new interaction experiences. Learn how to integrate keyboard and “delta” mouse coordinate events for player motion, and disable pointer system gestures like the Dock or Control Center to take full advantage of full screen gameplay.

    For further information on adding support for console game controllers like the Xbox Elite Wireless Controller Series 2 and Xbox Adaptive Controller, watch “Advancements in Game Controllers.” And learn more about using UIKit to manage indirect input by checking out “Handle trackpad and mouse input”.

    Recursos

    • Game Controller
      • Vídeo HD
      • Vídeo SD

    Vídeos relacionados

    WWDC21

    • Tap into virtual and physical game controllers

    WWDC20

    • Advancements in Game Controllers
    • Design for Game Center
    • Handle trackpad and mouse input
    • Support hardware keyboards in your app
  • Buscar neste vídeo...
    • 4:42 - Keyboard event change handlers

      if let keyboard = GCKeyboardDevice.coalesced?.keyboardInput {
      
        // bind to any key-up/-down
        keyboard.keyChangedHandler = {
          (keyboard, key, keyCode, pressed) in
          // compare buttons to GCKeyCode
        }
        
        // bind to a specific key-up/-down
        keyboard.button(forKeyCode: .spacebar)?.valueChangedHandler = {
          (key, value, pressed) in
          // spacebar was pressed or released
        }
      
      }
    • 5:18 - Polling keyboard state

      func pollInput() {
        
        if let keyboard = GCKeyboardDevice.coalesced?.keyboardInput {
      	  if (keyboard.button(forKeyCode: .keyW)?.isPressed ?? false) { /* move up    */ }
      	  if (keyboard.button(forKeyCode: .keyA)?.isPressed ?? false) { /* move left  */ }
      	  if (keyboard.button(forKeyCode: .keyS)?.isPressed ?? false) { /* move down  */ }
      	  if (keyboard.button(forKeyCode: .keyD)?.isPressed ?? false) { /* move right */ }
        }
        
      }
    • 6:05 - Mouse event change handlers

      if let mouse = GCMouse.currentMouse {
      
         mouse.mouseInput.mouseMovedHandler = {
           (mouse, deltaX, deltaY) in
           // use delta to calculate your game's cursor position or other motion
         }
      
      }
    • 7:27 - Pointer lock

      class ViewController: UIViewController {
          
          override var prefersPointerLocked: Bool {
              return true
          }
          
          override func viewDidLoad() {
              super.viewDidLoad()        
              self.setNeedsUpdateOfPrefersPointerLocked()
          }
      }
    • 8:07 - Discovery

      class ViewController: UIViewController {
        var keyboard: GCKeyboard? = nil
        var mouse: GCMouse? = nil
        
        init() {
          let center = NotificationCenter.defaultCenter
          let main = OperationQueue.mainQueue
              
          center.addObserverForName(GCMouseDidConnectNotification, object: nil, queue: main) {
            (note) in
            self.mouse = note.object as? GCMouse
          }
      
          center.addObserverForName(GCKeyboardDidConnectNotification, object: nil, queue: main) {
            (note) in
            self.keyboard = note.object as? GCKeyboard // the same as GCKeyboard.coalesced
          }
      
        }
      }
    • 10:56 - Updating Fox 2

      var delta: CGPoint = CGPoint.zero
      
      func registerMouse(_ mouseDevice: GCMouse) {
        
        if #available(iOS 14.0, OSX 10.16, *) {
          guard let mouseInput = mouseDevice.mouseInput else {
            return
          }
                
          // set up our mouse value change handlers
          
        }
      }
      
          weak var weakController = self
      
          mouseInput.mouseMovedHandler = {(mouse, deltaX, deltaY) in
            guard let strongController = weakController else {
              return
            }
                                          
            strongController.delta = CGPoint(x: CGFloat(deltaX), y: CGFloat(deltaY))
                                          
          }
                  
          mouseInput.leftButton.valueChangedHandler = {(button, value, pressed) in
            guard let strongController = weakController else {
              return
            }
                                                       
            strongController.controllerAttack()
                                                       
          }
                  
          mouseInput.scroll.valueChangedHandler = {(cursor, x, y) in
            guard let strongController = weakController else {
              return
            }          
            guard let camera = strongController.cameraNode.camera else {
              return
            }
                                                   
            camera.fieldOfView = CGFloat.maximum(CGFloat.minimum(120,
                                                 camera.fieldOfView + CGFloat(y)), 30)
      
            }
          }
      
      func pollInput() {
        ...
        
        // Mouse
        let mouseSpeed: CGFloat = 0.02
        self.cameraDirection += simd_make_float2(-Float(self.delta.x * mouseSpeed), 
                                                  Float(self.delta.y * mouseSpeed))
        self.delta = CGPoint.zero
              
        // Keyboard
        if let keyboard = GCKeyboard.coalesced?.keyboardInput {
          
            if (keyboard.button(forKeyCode: .keyA)?.isPressed ?? false) { self.characterDirection.x = -1.0 }
            if (keyboard.button(forKeyCode: .keyD)?.isPressed ?? false) { self.characterDirection.x = 1.0 }
            if (keyboard.button(forKeyCode: .keyW)?.isPressed ?? false) { self.characterDirection.y = -1.0 }
            if (keyboard.button(forKeyCode: .keyS)?.isPressed ?? false) { self.characterDirection.y = 1.0 }
                  
            self.runModifier = (keyboard.button(forKeyCode: .leftShift)?.value ?? 0.0) + 1.0
          
        }
      }

Developer Footer

  • Vídeos
  • WWDC20
  • Bring keyboard and mouse gaming to iPad
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines