Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Stop Default scrolling with arrows and space-bar, for JavaScript game

    Hi there,

    basically I have this Tetris game, with an all new GUI and all cool features. Which I transferred into a plug-in for a CMS. This means you can just drag and drop Tetris on your page!

    But the problem I have now, is that it uses keyboard functions: arrow keys, space-bar, ( p & n ).

    This means when there is a scrollbar on the page, the page also triggers the default scrolling behavior! Arrows down and up scroll the page a little bit, and Space-bar gets to the bottom of a page.


    Is there any way to stop keyboard input ( spacebar and arrows) from scrolling a page ?

    Maybe if I create a function that scrolls to the game every millisecond ( high CPU usage ? ) or create a function that keydown doesn't trigger any default behaviour.

    Or is it a lot easier by just adding focus to the element ? ( think blue-border in safari )

    Thanks before-hand!

    Jeroen R

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    in the body tag addstyle="overflow: hidden" this seems to work in all browsers except Firefox. to work around that you nead to put scroll(0,0)in a continuas loop:
    Code:
    function scroll()
    scroll(0,0);
    setTimeout("scroll()",1)//just scroll() will freeze the browser
    create a function that keydown doesn't trigger any default behaviour.
    modern browsers don't give JavaScript that type of access anymore, for security reasons
    Last edited by gizmo1650; 05-13-2010 at 12:22 AM.

  • Users who have thanked gizmo1650 for this post:

    JeroenR (05-13-2010)

  • #3
    New to the CF scene
    Join Date
    May 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thanks for the help!

    Hi there!

    Thanks for the helpful post, I think I might add a solution like this as well. For now I decided to go with 'WASD' and 'E' controls, which for some reason didn't come up in my mind before.

    Still I really appreciate the quick answer, and probably I'll add the option to choose for arrows

    Thanks!


  •  

    Tags for this Thread

    Posting Permissions

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