Educational materials

WiiLAB Example Code - isButtonPressed

Note: MATLAB R2007a or greater is required for this code to run correctly.

Annotations

The isButtonPressed function allows the user to tell if a button on the Wiimote is currently being pressed.

Usage

isButtonPressed('[Button]');

* where [Button] is one of the following strings: A, B, UP, RIGHT, DOWN, LEFT, PLUS, MINUS, HOME, ONE, TWO

Return

isButtonPressed will return true (1) if the button is pressed and false (0) if it is not.

Example

The following snippet of code will execute the block inside the if statement, and 'A is pressed on the Wiimote' will be displayed, if 'A' is pressed on the Wiimote at the time the function is called.

if ( isButtonPressed('A') )
    disp('A is pressed on the Wiimote');
end

Raw Source Code

The source code is also available as a .m file download at the bottom of the page.

function [buttonPressed]= isButtonPressed(ButtonToCheck)
%   usage:      isButtonPressed(ButtonToCheck)
%               Possible Buttons :: A B UP RIGHT DOWN LEFT PLUS MINUS HOME
%                                   ONE TWO
%   purpose:   Tests to see if a button is being pressed
   
global wiimote;
wiimote.GetButtonsState();

% check if the button is pressed based on the ButtonToCheck input
switch(upper(ButtonToCheck))
    case 'A'
        buttonPressed = wiimote.Buttons.A;
    case 'B'
        buttonPressed = wiimote.Buttons.B
    case 'UP'
        buttonPressed = wiimote.Buttons.Up;
    case 'RIGHT'
        buttonPressed = wiimote.Buttons.Right;
    case 'DOWN'
        buttonPressed = wiimote.Buttons.Down;
    case 'LEFT'
        buttonPressed = wiimote.Buttons.Left;
    case 'PLUS'
        buttonPressed = wiimote.Buttons.Plus;
    case 'MINUS'
        buttonPressed = wiimote.Buttons.Minus;
    case 'HOME'
        buttonPressed = wiimote.Buttons.Home;
    case 'ONE'
        buttonPressed = wiimote.Buttons.One;
    case 'TWO'
        buttonPressed = wiimote.Buttons.Two;
    otherwise
        % Bad input
        error('Bad input to isButtonPressed, must be one of the following: A B UP RIGHT DOWN LEFT PLUS MINUS HOME ONE TWO');
end   

-- JordanBrindza - 25 Jun 2008

This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Syndicate this site RSSATOM