CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2017
    Posts
    5

    How to assign function to keyboard function key in Visual Basic

    Hi,

    I'm trying to create a program in Visual Basic to assign a function key to print a screenshot of the current screen and another one to print a list of the files in the currently selected folder in File Explorer.

    The key assignments need to remain active even after the program that assigns the keys has been closed.

    Does anyone know how to do this?

    Debbie

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to assign function to keyboard function key in Visual Basic

    Have a look at the RegsiterHotKeyEx Windows API function

  3. #3
    Join Date
    Dec 2017
    Posts
    5

    Re: How to assign function to keyboard function key in Visual Basic

    Hi HanneSThEGreaT,

    Thanks for the pointer. I've tried looking for RegisterHotKeyEx in Visual Studio, but I can only find RegisterHotKey. Is that the one you meant?

    Also, I've discovered this page listing the Virtual Key Codes (https://msdn.microsoft.com/en-us/lib...v=vs.85).aspx), but when I try to use VK_F1, for instance, VS keeps saying it is not declared.

    Is there something need to import to get them declared?

    Debbie

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to assign function to keyboard function key in Visual Basic

    when I try to use VK_F1, for instance, VS keeps saying it is not declared.
    VK_F! is defined in winuser.h as a series of defines. To use these in VB you'll need to either use them as their numeric value or use equivalence assignments.

    Code:
    #define VK_F1             0x70
    #define VK_F2             0x71
    #define VK_F3             0x72
    #define VK_F4             0x73
    #define VK_F5             0x74
    #define VK_F6             0x75
    #define VK_F7             0x76
    #define VK_F8             0x77
    #define VK_F9             0x78
    #define VK_F10            0x79
    #define VK_F11            0x7A
    #define VK_F12            0x7B
    #define VK_F13            0x7C
    #define VK_F14            0x7D
    #define VK_F15            0x7E
    #define VK_F16            0x7F
    #define VK_F17            0x80
    #define VK_F18            0x81
    #define VK_F19            0x82
    #define VK_F20            0x83
    #define VK_F21            0x84
    #define VK_F22            0x85
    #define VK_F23            0x86
    #define VK_F24            0x87
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Dec 2017
    Posts
    5

    Re: How to assign function to keyboard function key in Visual Basic

    Hi 2kaud,

    Thanks for the reference, but how do I include winuser.h into my project, so I can use the constants.

    Also, could you explain what you mean by equivalence assignments, as I haven't heard that phrase before? Do you mean assiging the values to my own variables?

    Debbie

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to assign function to keyboard function key in Visual Basic

    but how do I include winuser.h into my project, so I can use the constants.
    I believe you have to define the variables yourself in the program - but I'm a c++ programmer, not a VB programmer so can't really help further.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured