www.webdeveloper.com
Results 1 to 7 of 7

Thread: How to "push" a button using javascript?

  1. #1
    Join Date
    Nov 2005
    Location
    India
    Posts
    336

    How to "push" a button using javascript?

    I am wondering if it is possible to simulate pushing a directional arrow key on the keyboard using javascript.

    If so could you please point me in the right direction?

    Thanks,
    klanga

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    What does the arrow key action do in the document?

  3. #3
    Join Date
    Nov 2005
    Location
    India
    Posts
    336
    I am trying to write javascript to play a flash snake game.

    I am a computer science major, but have never used javascript before. I know there is a timer function and if it is possible to "push" the arrow keys then the code should be pretty easy.

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    So here's the theory:

    1) User pushes a key
    2) You invoke a javascript function to do something

    But you're ahead of the game, you don't need to push the key, that's for silly physical entities that require some sort of input device to communicate with your awesome program, you just need to invoke the function at a certain time.

    This is what Fang was getting at, you don't try to perform the action, you just need to invoke the result.

    So, what is it you're trying to do?


    EDIT: I just noticed the word "flash" in your post. Not a good thing. Unless the Flash programmer has exposed some sort of API so it can communicate with Javascript, then there is absolutely nothing you can do (based on the last information that I have seen, anyway).
    Last edited by aj_nsc; 12-03-2010 at 01:52 PM.

  5. #5
    Join Date
    Nov 2005
    Location
    India
    Posts
    336
    There is not api for the game. It is just a snake game some site.

    I'm just trying to cheat the game.

    The only way I to interact with the game is through the keyboard.

    ...The more I think about it, this may be a program to write in c++ or java.

    This is just something to do for fun. Interesting though, yes?

  6. #6
    Join Date
    Dec 2005
    Posts
    2,984
    Haha, writing code for fun is always interesting...I love it, that's awesome. I hope you can figure out a way to do it. From what I know, though, flash can only listen to events directly and you can't send it any events (like keyboard input or mouse clicks) unless it is able to accept them through some API. That's my current level of understanding of flash and interaction with things like Javascript, I'm certainly open to being corrected by somebody more knowledgeable on the subject, however.

    Good luck.

  7. #7
    Join Date
    Nov 2005
    Location
    India
    Posts
    336
    Haha, It is working out nicely in c++. The only problem is getting everything times correctly and setting it up initially.

    If you are actually interested in helping here is what I have so far:
    Code:
    #include <windows.h>
    #include <time.h>
    #include <iostream>
    
    const static double LONG_TIME = 3890;
    const static double SHORT_TIME = LONG_TIME/2;
    
    using namespace std;
    
    void snake()
    {
    	cout << "Go!" << endl;
    	for(int j = 0; j < 409; j++)
    	{
    		for(int i = 0; i < 15; i++)
    		{
    			cout << i << endl;
    
    			keybd_event(VK_DOWN,0x28,0 , 0); // Down Arrow Press
    			keybd_event(VK_DOWN,0x28,KEYEVENTF_KEYUP,0); // Down Arrow Release
    			keybd_event(VK_RIGHT,0x27,0 , 0); // Right Arrow Press
    			keybd_event(VK_RIGHT,0x27,KEYEVENTF_KEYUP,0); // Right Arrow Release
    
    			Sleep(LONG_TIME);
    
    			keybd_event(VK_DOWN,0x28,0 , 0); // Down Arrow Press
    			keybd_event(VK_DOWN,0x28,KEYEVENTF_KEYUP,0); // Down Arrow Release
    			keybd_event(VK_LEFT,0x25,0 , 0); // Left Arrow Press
    			keybd_event(VK_LEFT,0x25,KEYEVENTF_KEYUP,0); // Left Arrow Release
    
    			Sleep(LONG_TIME);
    		}
    
    		keybd_event(VK_DOWN,0x28,0 , 0); // Down Arrow Press
    		keybd_event(VK_DOWN,0x28,KEYEVENTF_KEYUP,0); // Down Arrow Release
    		keybd_event(VK_RIGHT,0x27,0 , 0); // Right Arrow Press
    		keybd_event(VK_RIGHT,0x27,KEYEVENTF_KEYUP,0); // Right Arrow Release
    
    		Sleep(LONG_TIME+80);
    
    		keybd_event(VK_UP,0x26,0 , 0); // Up Arrow Press
    		keybd_event(VK_UP,0x26,KEYEVENTF_KEYUP,0); // Up Arrow Release
    
    		Sleep(SHORT_TIME);
    
    		keybd_event(VK_LEFT,0x25,0 , 0); // Left Arrow Press
    		keybd_event(VK_LEFT,0x25,KEYEVENTF_KEYUP,0); // Left Arrow Release
    
    		Sleep(LONG_TIME+80);
    	}
    }
    void main()
    {
    	system("PAUSE");
    
    
    	cout << endl << "10" << endl;
    	Sleep(1000);
    	cout << "9" << endl;
    	Sleep(1000);
    	cout << "8" << endl;
    	Sleep(1000);
    	cout << "7" << endl;
    	Sleep(1000);
    	cout << "6" << endl;
    	Sleep(1000);
    	cout << "5" << endl;
    	Sleep(1000);
    	cout << "4" << endl;
    	Sleep(1000);
    	cout << "3" << endl;
    	Sleep(1000);
    	cout << "2" << endl;
    	Sleep(1000);
    	cout << "1" << endl;
    	Sleep(1000);
    
    	snake();
    
    }

    Email me, because this definitely no longer belongs on this thread.
    al.reed13@gmail.com

    The grid is 32X64 and the snake grows 5 blocks each time you eat an apple. So the max possible score is 409.

    Thanks,
    Al

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles