creating a fake keydown event in Safari/Dashboard

Discussion in 'Mac Programming' started by Oats, Dec 19, 2006.

  1. macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #1
    Hi,
    I am trying to create a fake keydown event for a Dashboard widget I am creating. I am having extreme trouble trying to figure this out. Basically, I cannot seem to create the proper event structure. I want to create an event that simulates the user pressing the "return" key.

    Below is a quote from a page that seems to get me most of the way, though Safari does not seem to implement the initUIEvent routine:

    From http://www.howtocreate.co.uk/tutorials/javascript/domevents

     
  2. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    Oats, your posts always catch my attention.

    Why exactly do you need to emulate a keystroke?

    Sometimes knowing what you are trying to do could yield a more elegant and simple solution.
     
  3. thread starter macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #3
    thats funny, how come?

    Thanks for offering to help! I am developing a widget which uses a TEXTAREA element to use as a system console of sorts... I want users to be able to enter text at a prompt, and hit enter and receive a text response, in the same textarea. I have all this working fine in Firefox, the one problem I am having currently is when I insert text beyond the limits of the visible TEXTAREA, I want the TEXTAREA to scroll to the bottom automatically. It is quite frustrating without this, since the user will hit "return" to get a response, but the response is hidden until the user starts typing again.

    My original thought (which works in firefox) was to set the scroll position of the TEXTAREA element, but this does not seem to be supported in Safari. (See related thread: http://forums.macrumors.com/showthread.php?t=260058)

    My next thought was to create a "pagedown" or "end" keydown event, which would also scroll to the end of the TEXTAREA. Unfortunately, this doesn't seem to be well supported in Safari either, as far as I can tell. I can find all sorts of documentation about how to do this with IE or Firefox, but nothing for Safari. In my limited experience, Safari is extremely quirky about it's javascript implementation. Another reason why I think it is a shame that Safari even exists. Apple should just junk safari, and adopt Firefox. IMHO.
     
  4. macrumors regular

    Joined:
    Jul 15, 2004
    Location:
    Michigan
    #4
    I have a feeling you should go back to this approach; make a handler call after adding your response text to the textarea.

    Only this time, instead of trying to scroll the textarea directly, simply 'select' the last character of text and replace it with itself. This should (meaning I haven't tested it) trick Safari into scrolling to wherever the final character is. That should have the desired effect without trying to trick safari into thinking there's keyboard presses going on.

    Another thing you might try is a double-pane approach, where there's a bottom-half (or quarter... whatever) textarea that takes input, and a top-half that displays response texts.

    Rock!

    -rand()
     
  5. thread starter macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #5
    that doesn't work. any other ideas?
     
  6. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #6
    Seems your only option may be a scrollable DIV.
     
  7. thread starter macrumors regular

    Joined:
    Jan 8, 2003
    Location:
    New York
    #7
    using a scrollable DIV would work for the scroll issue, but then I don't think I can accept user input like a terminal?
     
  8. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #8
    you can, but it gets more complicated with having to dynamically use a text input box.

    This just isn't going to be easy :(

    Good Luck.
     
  9. macrumors newbie

    Joined:
    Aug 22, 2012
    #9
    document.onkeypress not working

    document.onkeypress not working after dispatching the event. Only onkeydown is working. What should is suppose to do for making both working
     

Share This Page