Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Keypress - How to set the key it reacts to?

Boogy_ManBoogy_Man Member Posts: 4
How would you make something occure when say you press the left arrow
«1

Comments

  • mfroebmfroeb Member Posts: 53
    : How would you make something occure when say you press the left arrow
    :
    in the form_keypress event, test the keyascii parameter:

    private sub form_keypress(keyascii as integer)
    if keyascii = vbkeyleft then
    ' your code here ...
    end if
    end sub
  • Boogy_ManBoogy_Man Member Posts: 4
    is there anything else i have to put anywhere to make it work.
    it doesn't work the way it is.
  • mfroebmfroeb Member Posts: 53
    : is there anything else i have to put anywhere to make it work.
    : it doesn't work the way it is.
    :

    the form_keypress event is only raised when the form has the keyboard focus _OR_ the keypreview setting of the form is set to true (in the property manager). else the xyz_keypress event of the control with the focus is raised.
  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : : How would you make something occure when say you press the left arrow
    : :
    : in the form_keypress event, test the keyascii parameter:
    :
    : private sub form_keypress(keyascii as integer)
    : if keyascii = vbkeyleft then
    : ' your code here ...
    : end if
    : end sub
    :

    vbKeyLeft will not trigger _KeyPress event.

    Technically, none of the vbKey* constants will; some of them just happen to match.

    [b]Set KeyPreview (on the form) to True.
    Use the KeyDown event.[/b]
  • mfroebmfroeb Member Posts: 53
    : : : How would you make something occure when say you press the left arrow
    : : :
    : : in the form_keypress event, test the keyascii parameter:
    : :
    : : private sub form_keypress(keyascii as integer)
    : : if keyascii = vbkeyleft then
    : : ' your code here ...
    : : end if
    : : end sub
    : :
    :
    : vbKeyLeft will not trigger _KeyPress event.
    :
    : Technically, none of the vbKey* constants will; some of them just happen to match.
    :
    : [b]Set KeyPreview (on the form) to True.
    : Use the KeyDown event.[/b]
    :

    "vbKeyLeft will not trigger _KeyPress event."
    no constant can trigger an event, that's technically incorrect.

    "some of them just happen to match"
    All vbKey* constants not corresponding to control characters (such as above 32) are just integer representations of the ascii keycode and are therefore also correct for the *_keypress(key_ascii_) event, and _should_ therefore also match.

    i have used this constants always, and it always has worked for me. blame microsoft.

    [b]but using the keydown event is right. thank you for this hint.[/b]
  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : : : : How would you make something occure when say you press the left arrow
    : : : :
    : : : in the form_keypress event, test the keyascii parameter:
    : : :
    : : : private sub form_keypress(keyascii as integer)
    : : : if keyascii = vbkeyleft then
    : : : ' your code here ...
    : : : end if
    : : : end sub
    : : :
    : :
    : : vbKeyLeft will not trigger _KeyPress event.
    : :
    : : Technically, none of the vbKey* constants will; some of them just happen to match.
    : :
    : : [b]Set KeyPreview (on the form) to True.
    : : Use the KeyDown event.[/b]
    : :
    :
    : "vbKeyLeft will not trigger _KeyPress event."
    : no constant can trigger an event, that's technically incorrect.
    :
    : "some of them just happen to match"
    : All vbKey* constants not corresponding to control characters (such as above 32) are just integer representations of the ascii keycode and are therefore also correct for the *_keypress(key_ascii_) event, and _should_ therefore also match.
    :
    : i have used this constants always, and it always has worked for me. blame microsoft.
    :
    : [b]but using the keydown event is right. thank you for this hint.[/b]
    :

    I use them as well since they do match. But the keyboard can be remapped which would possibly break code written this way...
  • mfroebmfroeb Member Posts: 53
    : : : : : How would you make something occure when say you press the left arrow
    : : : : :
    : : : : in the form_keypress event, test the keyascii parameter:
    : : : :
    : : : : private sub form_keypress(keyascii as integer)
    : : : : if keyascii = vbkeyleft then
    : : : : ' your code here ...
    : : : : end if
    : : : : end sub
    : : : :
    : : :
    : : : vbKeyLeft will not trigger _KeyPress event.
    : : :
    : : : Technically, none of the vbKey* constants will; some of them just happen to match.
    : : :
    : : : [b]Set KeyPreview (on the form) to True.
    : : : Use the KeyDown event.[/b]
    : : :
    : :
    : : "vbKeyLeft will not trigger _KeyPress event."
    : : no constant can trigger an event, that's technically incorrect.
    : :
    : : "some of them just happen to match"
    : : All vbKey* constants not corresponding to control characters (such as above 32) are just integer representations of the ascii keycode and are therefore also correct for the *_keypress(key_ascii_) event, and _should_ therefore also match.
    : :
    : : i have used this constants always, and it always has worked for me. blame microsoft.
    : :
    : : [b]but using the keydown event is right. thank you for this hint.[/b]
    : :
    :
    : I use them as well since they do match. But the keyboard can be remapped which would possibly break code written this way...
    :

    have you ever seen a remapped keyboard? and in what way "remapped"?
    every keyboard sends a manufacturer-defined scancode if a key is pressed. windows then translates this key (with help of keyboard driver) into a windows key constant. they always stay the same, and are also the ones you get from visual basic. so what do you mean with "remapped"?

    btw: don't fortget to visit [red]aac.net.tf[/red] for the best audio cd ripper for windows!
  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : have you ever seen a remapped keyboard? and in what way "remapped"?
    : every keyboard sends a manufacturer-defined scancode if a key is pressed. windows then translates this key (with help of keyboard driver) into a windows key constant. they always stay the same, and are also the ones you get from visual basic. so what do you mean with "remapped"?
    :
    : btw: don't fortget to visit [red]aac.net.tf[/red] for the best audio cd ripper for windows!
    :

    Remapped means to change which keys do what. I ran across a program once that did just that. Lots of fun to mess with people's heads when half the keyboard is remapped to the Ctrl key.

    Seriously though, the scancode and the key constant are "linked" together. Of course they stay the same. But you could make an app to remap a person's keyboard so that they get a "customized" character when they press a key. The scancode (and therefor the keycode) would still be the exact same, whereas the resulting character (ASCII values) would have been changed.

    It's not one of those things you are likely to run across (like .TwipsPerPixel* being anything other than 15), but it is still possible.
  • mfroebmfroeb Member Posts: 53
    : : have you ever seen a remapped keyboard? and in what way "remapped"?
    : : every keyboard sends a manufacturer-defined scancode if a key is pressed. windows then translates this key (with help of keyboard driver) into a windows key constant. they always stay the same, and are also the ones you get from visual basic. so what do you mean with "remapped"?
    : :
    : : btw: don't forget to visit [red]aac.net.tf[/red] for the best audio cd ripper for windows!
    : :
    :
    : Remapped means to change which keys do what. I ran across a program once that did just that. Lots of fun to mess with people's heads when half the keyboard is remapped to the Ctrl key.
    :
    : Seriously though, the scancode and the key constant are "linked" together. Of course they stay the same. But you could make an app to remap a person's keyboard so that they get a "customized" character when they press a key. The scancode (and therefor the keycode) would still be the exact same, whereas the resulting character (ASCII values) would have been changed.
    :
    : It's not one of those things you are likely to run across (like .TwipsPerPixel* being anything other than 15), but it is still possible.
    :

    thanks for your information. i really didn't know that this is possible. could you explain how to remap the keyboard to me? i'd be very happy to know this ;-)
  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : thanks for your information. i really didn't know that this is possible. could you explain how to remap the keyboard to me? i'd be very happy to know this ;-)
    :

    I would love to if I knew myself; I'd have use for it. I've only run across one program that did it and that was a few years ago. I suppose that a search for keyboard and map or remap might turn something up, but I've never gotten around to looking for it again...
«1
Sign In or Register to comment.