Struct ctru::services::hid::Hid

source ·
pub struct Hid { /* private fields */ }
Expand description

Handle to the HID service.

Implementations§

source§

impl Hid

source

pub fn new() -> Result<Hid>

Initialize a new service handle.

§Errors

This function will return an error if the service was unable to be initialized. Since this service requires no special or elevated permissions, errors are rare in practice.

§Example
use ctru::services::hid::Hid;

let hid = Hid::new()?;
source

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();
source

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!")
}
source

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!")
}
source

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!")
}
source

pub fn touch_position(&self) -> (u16, u16)

Returns the current touch position in pixels (x, y).

§Notes

(0, 0) represents the top left corner of the screen.

§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;

hid.scan_input();

let (touch_x, touch_y) = hid.touch_position();
source

pub fn circlepad_position(&self) -> (i16, i16)

Returns the current circle pad position in relative (x, y).

§Notes

(0, 0) represents the center of the circle pad.

§Example
use ctru::services::hid::Hid;
let mut hid = Hid::new()?;

hid.scan_input();

let (pad_x, pad_y) = hid.circlepad_position();
source

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();
source

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();
source

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();
source

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()?;
source

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()?;

Auto Trait Implementations§

§

impl !RefUnwindSafe for Hid

§

impl !Send for Hid

§

impl Sync for Hid

§

impl Unpin for Hid

§

impl !UnwindSafe for Hid

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.