I'd like to know how can I perform f开发者_JS百科or example a right mouse click, or press ENTER with XNA, is it possible? I want the XNA prog to do the action, not to check if I clicked it. Thanks.
You can use the following to make the cursor of the mouse visible (if this is necessary):
protected override void Initialize()
{
// Make mouse visible
this.IsMouseVisible = true;
base.Initialize();
}
Create the following variables, this way you can save the current and previous mouseState:
MouseState mouseStateCurrent, mouseStatePrevious;
In the Update function you can use the following:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
// Get current mouseState
mouseStateCurrent = Mouse.GetState();
// Left MouseClick
if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
// TODO when left mousebutton clicked
}
// Right MouseClick
if (mouseStateCurrent.RightButton == ButtonState.Pressed && mouseStatePrevious.RightButton == ButtonState.Released)
{
//TODO when right mousebutton clicked
}
mouseStatePrevious = mouseStateCurrent;
// Update
base.Update(gameTime);
}
Hopefully this is of some use to you!
Just call the function that clicking would call
So leftClick gets called when the user clicks, but you can call that function whenever you want
edit: Another thing you can do if you can do if you want to call mouseClick yourself (instead of leftClick) is to have your own variable sent in, and have an enum for different states you want to define.
enum CLICKS{left=0, right, middle);
public void mouseClick(){
mouseClick(-1);
}
public void mouseClick(int manualClick){
if(mouseStateCurrent.LeftButton == ButtonState.Pressed || manualClick == CLICKS.left)
leftClick();
}
public void leftClick(){
//do stuff
}
public void randomFunction(){
//doing stuff
mouseClick(CLICKS.left); //<-- will simulate a left click
}
If you'd like to simulate real mouse pressed, you can use the SendInput Win32 method.
Ths is not as easy as the technique already offered here, but the game will not be able to distinguish whether these clicks originated from real user clicks or simulated ones.
Also check out this StackOverflow question that discussed the same issue.
精彩评论