How to detect that a key is being pressed, not HAS been pressed earlier!??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rune

    How to detect that a key is being pressed, not HAS been pressed earlier!??

    Hey

    I'm trying to build a gui application and need to know if the user is
    actually holding down the shift or ctrl key. That is, if the user
    currently is holding down the shift key. In pseudo code this will boil
    down to something like this:

    def test:
    if user presses shift:
    return SHIFT_IS_PRESSE D
    elif user presses ctrl:
    return CTRL_IS_PRESSED
    else
    return false

    It's important to notice here that I'm not interested if the user has
    already pressed shift or ctrl. I'm only interested in knowing if he is
    currently holding down one of these keys. (I have looked into msvcrt
    and the like but have found no answer..) The function should also work
    in both windows and Linux.

    Any help is appriciated :)
  • Gilles Lenfant

    #2
    Re: How to detect that a key is being pressed, not HAS been pressed earlier!??

    "Rune" <runed@stud.cs. uit.no> a écrit dans le message de
    news:6ed33425.0 401281103.61987 e72@posting.goo gle.com...[color=blue]
    > Hey
    >
    > I'm trying to build a gui application and need to know if the user is
    > actually holding down the shift or ctrl key. That is, if the user
    > currently is holding down the shift key. In pseudo code this will boil
    > down to something like this:
    >
    > def test:
    > if user presses shift:
    > return SHIFT_IS_PRESSE D
    > elif user presses ctrl:
    > return CTRL_IS_PRESSED
    > else
    > return false
    >
    > It's important to notice here that I'm not interested if the user has
    > already pressed shift or ctrl. I'm only interested in knowing if he is
    > currently holding down one of these keys. (I have looked into msvcrt
    > and the like but have found no answer..) The function should also work
    > in both windows and Linux.
    >
    > Any help is appriciated :)[/color]

    You should have a look at the wxPython demo (wxKeyEvents section) that shows
    how this is handled in a cross platform fashion.

    Search for wxPython from www.sf.net

    --
    Gilles


    Comment

    • Jarek Zgoda

      #3
      Re: How to detect that a key is being pressed, not HAS been pressed earlier!??

      Rune <runed@stud.cs. uit.no> pisze:
      [color=blue]
      > It's important to notice here that I'm not interested if the user has
      > already pressed shift or ctrl. I'm only interested in knowing if he is
      > currently holding down one of these keys. (I have looked into msvcrt
      > and the like but have found no answer..) The function should also work
      > in both windows and Linux.[/color]

      This event *is* available in WinAPI, but don't ask me where. I'm sure,
      because VCL (which is built on top of WinAPI and uses standard Windows
      messages) distinguishes KeyUp and KeyDown events.

      If you want to have it available also in linux, use Qt/PyQt.

      --
      Jarek Zgoda
      Unregistered Linux User #-1
      http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/

      Comment

      • Thomas Heller

        #4
        Re: How to detect that a key is being pressed, not HAS been pressedearlier! ??

        runed@stud.cs.u it.no (Rune) writes:
        [color=blue]
        > Hey
        >
        > I'm trying to build a gui application and need to know if the user is
        > actually holding down the shift or ctrl key. That is, if the user
        > currently is holding down the shift key. In pseudo code this will boil
        > down to something like this:
        >
        > def test:
        > if user presses shift:
        > return SHIFT_IS_PRESSE D
        > elif user presses ctrl:
        > return CTRL_IS_PRESSED
        > else
        > return false
        >
        > It's important to notice here that I'm not interested if the user has
        > already pressed shift or ctrl. I'm only interested in knowing if he is
        > currently holding down one of these keys. (I have looked into msvcrt
        > and the like but have found no answer..) The function should also work
        > in both windows and Linux.[/color]

        No idea about linux, but on windows you use win32api.GetKey State().

        Thomas

        Comment

        • Jeff Epler

          #5
          Re: How to detect that a key is being pressed,not HAS been pressed earlier!??

          In Tk, each event has a 'state' field, which can tell you whether
          modifier keys or mouse buttons were pressed when an event was received.
          Note that for the KeyPress event which is a modifier key, the state
          field will not reflect the newly pressed key (the same goes for button
          presses), as implied by this section of the XKeyEvent manpage:

          The state member is set to indicate the logical state of the pointer
          buttons and modifier keys just prior to the event, which is the bitwise
          inclusive OR of one or more of the button or modifier key masks: But-
          ton1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask, Shift-
          Mask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask,
          and Mod5Mask.

          from Tkinter import *

          # for pre-2.3 versions
          #def enumerate(l):
          # for i in range(len(l)): yield i, l[i]

          # Set up a mapping from bits to names
          # (I'm not sure if these values match on Windows)
          names = "Shift Caps Control Alt Num Mod3 Mod4 Mod5 1 2 3 4 5".split()
          mods = [(1<<i, v) for i, v in enumerate(names )]

          # Here's a handler that gets various events and looks at the state field
          def setModifiers(ev ent):
          s = []
          for i, v in mods:
          if event.state & i: s.append(v)
          if not s: s = ['(none)']
          var.set(" ".join(s))

          # Set up a minimal user interface
          t = Tk()
          var = StringVar(t)
          l = Label(t, textv = var, width=32)
          l.pack()

          # including a couple of bindings (the text will update when
          # one of these bindings is triggered)
          for binding in "<Motion> <1> <ButtonReleas e-1>".split():
          l.bind(binding, setModifiers)
          t.mainloop()

          Jeff

          Comment

          • Sean Richards

            #6
            Re: How to detect that a key is being pressed, not HAS been pressedearlier! ??

            runed@stud.cs.u it.no (Rune) writes:
            [color=blue]
            > Hey
            >
            > I'm trying to build a gui application and need to know if the user is
            > actually holding down the shift or ctrl key. That is, if the user
            > currently is holding down the shift key. In pseudo code this will boil
            > down to something like this:
            >
            > def test:
            > if user presses shift:
            > return SHIFT_IS_PRESSE D
            > elif user presses ctrl:
            > return CTRL_IS_PRESSED
            > else
            > return false
            >
            > It's important to notice here that I'm not interested if the user has
            > already pressed shift or ctrl. I'm only interested in knowing if he is
            > currently holding down one of these keys. (I have looked into msvcrt
            > and the like but have found no answer..) The function should also work
            > in both windows and Linux.
            >
            > Any help is appriciated :)[/color]

            In wxPython you could do it like this ...

            1. Bind the key press events to the two functions

            EVT_KEY_DOWN(se lf, self.OnKeyDown)
            EVT_KEY_UP(self , self.OnKeyUp)

            2. Set or unset a flag on keydown and keyup

            def OnKeyDown(self, event):
            if (event.GetKeyCo de() == WXK_SHIFT):
            self.Shift = True
            elif (event.GetKeyCo de() == WXK_CONTROL):
            self.Control = True

            def OnKeyUp(self, event):
            if (event.GetKeyCo de() == WXK_SHIFT):
            self.Shift = False
            elif (event.GetKeyCo de() == WXK_CONTROL):
            self.Control = False

            Vennlig hilsen, Sean


            --
            "Hver sin smak", sa vintapperen, han drakk mens de andre sloss.

            Comment

            • Richie Hindle

              #7
              Re: How to detect that a key is being pressed,not HAS been pressed earlier!??


              [Rune][color=blue]
              > It's important to notice here that I'm not interested if the user has
              > already pressed shift or ctrl. I'm only interested in knowing if he is
              > currently holding down one of these keys.[/color]

              [Thomas][color=blue]
              > No idea about linux, but on windows you use win32api.GetKey State().[/color]

              You mean GetAsyncKeyStat e: "The GetAsyncKeyStat e function determines
              whether a key is up or down at the time the function is called"

              Not GetKeyState: "The key status returned from this function changes as a
              thread reads key messages from its message queue. The status does not
              reflect the interrupt-level state associated with the hardware."

              ....assuming, Rune, that you *really do* want the current state. If your
              program is event driven, and you're responding to Shift+Click or Ctrl+Drag
              or similar, then you want GetKeyState. Imagine the machine is running
              very slowly. I Shift+Click your app and release the Shift key before your
              app responds. You call GetAsyncKeyStat e and see that Shift is not
              pressed. Had you used GetKeyState, it would tell you that the Shift key
              was pressed at the time of the click.

              --
              Richie Hindle
              richie@entrian. com


              Comment

              Working...