简体   繁体   中英

Call java method in JSF/xhtml by keyPress and update form

Hi I got school project to code game in JSF but I struggle o lot with keyboard.
I want to call method moveCubeUp() from cubefield to move UP if user press keyboard UP and then update form and do this to all 4 directions.
I tried javascripts, jQuery, primefaces hotkey

This is in my xhtml:

                <div class="game">
                    <h:form id="form">
                        <g:cuberoll value="#{cubefield}"/>

                        Cube X: <h:outputLabel value="#{cubefield.getCubeX()}"/>
                        Cube Y: <h:outputLabel value="#{cubefield.getCubeY()}"/>
                    </h:form>
                </div>

and cubefield:

@Named("cubefield")
@SessionScoped
public class Field implements Serializable{
public Field() {
   ... constructor
}

public void moveCubeRight(){
        ... do stuff
}

I think you can try to trigger it with ajax. It should look like this :

    `<f:ajax event="onkeypress" execute="keyCode" listener="#{bean.listener}" />`

Then you can get the key in an hidden input.

  `<h:inputHidden id="keyCode" binding="#{keyCode}" value="#{bean.keyCode}" />`  

This topic is related to what you are asking : Catch KeyCode in AjaxBehaviorEvent of JSF 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM