pub struct Hid { /* private fields */ }
Expand description
Handle to the HID service.
Implementations§
source§impl Hid
impl Hid
sourcepub fn scan_input(&mut self)
pub fn scan_input(&mut self)
Scan the HID service for all user input occurring on the current frame.
This function should be called on every frame when polling for user input.
§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;
hid.scan_input();
sourcepub fn keys_down(&self) -> KeyPad
pub fn keys_down(&self) -> KeyPad
Returns a bitflag struct representing which buttons have just been pressed on the current frame (and were not pressed on the previous frame).
§Example
use ctru::services::hid::{Hid, KeyPad};
let mut hid = Hid::new()?;
hid.scan_input();
if hid.keys_down().contains(KeyPad::A) {
println!("You have pressed the A button!")
}
sourcepub fn keys_held(&self) -> KeyPad
pub fn keys_held(&self) -> KeyPad
Returns a bitflag struct representing which buttons have been held down during the current frame.
§Example
use ctru::services::hid::{Hid, KeyPad};
let mut hid = Hid::new()?;
hid.scan_input();
if hid.keys_held().contains(KeyPad::START) {
println!("You are holding the START button!")
}
sourcepub fn keys_up(&self) -> KeyPad
pub fn keys_up(&self) -> KeyPad
Returns a bitflag struct representing which buttons have just been released on the current frame.
§Example
use ctru::services::hid::{Hid, KeyPad};
let mut hid = Hid::new()?;
hid.scan_input();
if hid.keys_held().contains(KeyPad::B) {
println!("You have released the B button!")
}
sourcepub fn touch_position(&self) -> (u16, u16)
pub fn touch_position(&self) -> (u16, u16)
sourcepub fn circlepad_position(&self) -> (i16, i16)
pub fn circlepad_position(&self) -> (i16, i16)
sourcepub fn volume_slider(&self) -> f32
pub fn volume_slider(&self) -> f32
Returns the current volume slider position (between 0 and 1).
§Notes
The ndsp
service automatically uses the volume slider’s position to handle audio mixing.
As such this method should not be used to programmatically change the volume.
Its purpose is only to inform the program of the volume slider’s position (e.g. checking if the user has muted the audio).
§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;
hid.scan_input();
let volume = hid.volume_slider();
sourcepub fn set_accelerometer(&mut self, enabled: bool) -> Result<()>
pub fn set_accelerometer(&mut self, enabled: bool) -> Result<()>
Activate/deactivate the console’s acceleration sensor.
§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;
// The accelerometer will start to register movements.
hid.set_accelerometer(true).unwrap();
sourcepub fn set_gyroscope(&mut self, enabled: bool) -> Result<()>
pub fn set_gyroscope(&mut self, enabled: bool) -> Result<()>
Activate/deactivate the console’s gyroscopic sensor.
§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;
// The gyroscope will start to register positions.
hid.set_gyroscope(true).unwrap();
sourcepub fn accelerometer_vector(&self) -> Result<Acceleration, Error>
pub fn accelerometer_vector(&self) -> Result<Acceleration, Error>
Returns the acceleration vector (x, y, z) registered by the accelerometer.
§Errors
This function will return an error if the accelerometer was not previously enabled.
Have a look at Hid::set_accelerometer()
to enable the accelerometer.
§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;
// The accelerometer will start to register movements.
hid.set_accelerometer(true).unwrap();
// It's necessary to run `scan_input()` to update the accelerometer's readings.
hid.scan_input();
// This call fails if the accelerometer was not previously enabled.
let acceleration = hid.accelerometer_vector()?;
sourcepub fn gyroscope_rate(&self) -> Result<AngularRate, Error>
pub fn gyroscope_rate(&self) -> Result<AngularRate, Error>
Returns the angular rate registered by the gyroscope.
§Errors
This function returns an error if the gyroscope was not previously enabled.
Have a look at Hid::set_gyroscope()
.
§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;
// The gyroscope will start to register positions.
hid.set_gyroscope(true).unwrap();
// It's necessary to run `scan_input()` to update the gyroscope's readings.
hid.scan_input();
// This call fails if the gyroscope was not previously enabled.
let angular_rate = hid.gyroscope_rate()?;