How to work with button presses

Discussion in 'Approved' started by Selbi, Nov 30, 2009.

Thread Status:
Not open for further replies.
  1. Selbi

    Selbi Plug in, baby. Retired Staff

    Joined:
    Jul 20, 2008
    Messages:
    2,398
    Location:
    Northern Germany
    How to work with button presses


    I always had problems with button presses, until I found this post. Though I can just search for "Button Press" every time I need that information, I think it's a better idea to make a real guide out of it (also, I'm bored right now :V).


    To make a button test, for example if you want a check for the button B (this will be the example in this guide), you need to choose which method you wanna use: Is the button being held or just being pressed? will continue with the held one. (If you want the code for the pressed one, scroll down a bit.)


    Check if a Button is being held


    For this you will need the RAM adress ($FFFFF602).w, as it's the one for the held buttons. It works with a btst command (BiTeST), which actually does what the name suggests: Checking if a specific bit in a specific RAM adress is set or not. Choose the button you want in this list:



    Start = 7
    A = 6


    C = 5


    B = 4


    Right = 3


    Left = 2


    Down = 1


    Up = 0



    An example with the button B would be:



    btst #4,($FFFFF602).w ; check if B is being held
    bne.s IfBIsBeingHeld ; if so, branch



    In case you don't know how to work with branches, you just need to change the bne.s to a beq.s to check for the opposite:



    btst #4,($FFFFF602).w ; check if B is being held
    beq.s IfBIsNotBeingHeld ; if not, branch



    And that's pretty much it.


    Check if a Button is being pressed


    This one doesn't work with btst, but with and.b. I don't really know how that works, but shobiz does, and I don't think he's lying (=P). Anyway, in case you don't know the difference between a button being held and pressed: While held checks if you are keep holding a specific button and keeps redoing your code until you release the button, pressing just does your code one time, no matter how long you hold a button. For this one you will need the RAM address ($FFFFF603).w (basically the next one from held). Again, choose the button you want from this list:



    Start = $80
    ABC = $70


    A = $40


    C = $20


    B = $10


    Right = $08


    Left = $04


    Down = $02


    Up = $01



    An example with button B again would be:



    move.b ($FFFFF603).w,d0 ; move the current button press to d0 (we may not and d0 directly for whatever reason)
    and.b #$10,d0 ; and the your value on it ($10/B in this case)


    bne.s IfBIsBeingPressed ; if B is being pressed, branch



    The version if B is not being pressed:



    move.b ($FFFFF603).w,d0 ; move the current button press to d0 (we may not and d0 directly for whatever reason)
    and.b #$10,d0 ; and the your value on it ($10/B in this case)


    beq.s IfBIsNotBeingPressed ; if B is not being pressed, branch



    And there you go! Have fun with making your own... button presses... whatever.


    Credits:


    - Me (Selbi) for the guide


    - shobiz for the original guide and actually the whole information
     
    Last edited by a moderator: Jan 8, 2010
  2. amphobius

    amphobius spreader of the pink text Member

    Joined:
    Feb 24, 2008
    Messages:
    971
    Location:
    United Kingdom
    Actually, instead of picking numbers, a set of macros can be made (I'll talk about it and stuff to you in private), but nevertheless, a good explaination. :(
     
  3. Spanner

    Spanner The Tool Administrator

    Joined:
    Aug 9, 2007
    Messages:
    2,345
    He could also use that list as equates if they're not in use:



    btst B,($FFFFF602).w ; check if B is being held



    The SVN version already implements this I believe.
     
Thread Status:
Not open for further replies.