Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my previous question (Method must have a return type in C# Class[^]) I used that class to make Global Hotkeys. But when I'm trying to use it, I can't get access to the Constants, which is a public static class; An error is shown: The name "Constants does not exist in the current context.

This is the code for "using" the global hotkeys class(it is used like that in the DreamInCode post http://www.dreamincode.net/forums/topic/180436-global-hotkeys/[^])

C#
namespace Quick_Preview
{
    public partial class Form1 : Form
    {
        private GlobalHotkeys.GBH ghk;
        public Form1()
        {
            InitializeComponent();
            ghk = new GlobalHotkeys.GBH(Constants.NOMOD, Keys.Space, this);
        }
    }
}
//Constants is what "does not exists"


I will also show the complete class code(Which does not show any errors):
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GlobalHotkeys
{
    public class GBH
    {
        public void GlobalHotkey(int modifier, Keys key, Form form)
        {
            this.modifier = modifier;
            this.key = (int)key;
            this.hWnd = form.Handle;
            id = this.GetHashCode();

        }

        public static class Constants
        {
            //modifiers
            public const int NOMOD = 0x0000;
            public const int ALT = 0x0001;
            public const int CTRL = 0x0002;
            public const int SHIFT = 0x0004;
            public const int WIN = 0x0008;

            //window message id for hotkey
            public const int WM_HOTKEY_MSG_ID = 0x0312;
        }

        private int modifier;
        private int key;
        private IntPtr hWnd;
        private int id;

        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);



        public override int GetHashCode()
        {
            return modifier ^ key ^ hWnd.ToInt32();
        }

        public bool Register()
        {
            return RegisterHotKey(hWnd, id, modifier, key);
        }

        public bool Unregister()
        {
            return UnregisterHotKey(hWnd, id);
        }
    }
}


[EDIT]
This is what I'm trying to do:
http://i.i.cbsi.com/cnwk.1d/i/tim/2012/01/09/QuickLookExample.png[^]
That happens when you press Space with a file selected in Mac OSX. If you press Space again, that "form" minimizes. And it does not show in the mac os x app launcher. That's exactly the same I want to do, but a title bar shows in the bottom corner of my screen, thing that should not happen.
Posted
Updated 10-Apr-15 13:48pm
v2

It looks like everything is correct. All you might have missing is correct naming of the constant. (Again, please review my previous answer to the question you referenced, in part of namespaces.)

This is how you can use this constant in the most general case, independent from content. It means, it could be done in the code of any type at all in your assembly or any assembly using your assembly (due to public access modifiers on both class and members):

C#
int ctrlPressed = GlobalHotkeys.GBH.Constants.CTRL;
int ctrlAltPressed =
   GlobalHotkeys.GBH.Constants.CTRL |
   GlobalHotkeys.GBH.Constants.ALT;


Note that your use of nested class (Constant nested in GBH) adds its own part to namespace and the full type name.

And this is how you could use using alias:
C#
using Constants = GlobalHotkeys.GBH.Constants;
// instead of Constants, it could be any other suitable name

// ...

int ctrlPressed = Constants.CTRL;
int ctrlAltPressed =
   Constants.CTRL |
   Constants.ALT;


[EDIT]

Do yourself a big favor, just ignore the advice in Solution 2. There is nothing wrong with putting anything is a separate file or on upper layer, but this is merely a way to avoid understanding how things really work, which is, in this case, a very simple thing.

—SA
 
Share this answer
 
v4
Comments
ChrisCreateBoss 10-Apr-15 18:34pm    
Thank you so much, this really helped me a lot.
Sergey Alexandrovich Kryukov 10-Apr-15 18:35pm    
You are very welcome.
Good luck, call again.
—SA
ChrisCreateBoss 10-Apr-15 18:53pm    
Hey something more, I don't want to show my app in the taskbar, that's done already, but when I minimize it, a title bar stays in the left bottom corner of the screen, how can I turn to "false" that?
Sergey Alexandrovich Kryukov 10-Apr-15 19:14pm    
May I ask you why would you need it? You see, if some application is not represented anywhere on desktop at all, you don't have access to is, would not be able to show it again.
It looks like what you want makes no sense. Even not showing in a task bar — why? This option is needed only if you have multiple form and want only the main application form to show...
—SA
ChrisCreateBoss 10-Apr-15 19:23pm    
As I said in the previous question, I'm trying to do Mac Os X's preview app. That app does not show anywhere as a separate software. So my app shows when pressing space, and it hides when pressing space again.
ChrisCreateBoss asked

Hey something more, I don't want to show my app in the taskbar, that's done already, but when I minimize it, a title bar stays in the left bottom corner of the screen, how can I turn to "false" that?
Please see my comment to your question in comments to Solution 1.

It just would not make any sense. If you hide your application from the desktop, you would not have access to it at all, you could only kill the process using Task Manager or, say, use some global hotkey (if you are interested, ask me how, right now it would be off-topic). You need something to show in a desktop.

—SA
 
Share this answer
 
Comments
ChrisCreateBoss 10-Apr-15 19:30pm    
As I said in the previous question, I'm trying to do Mac Os X's preview app. That app does not show anywhere as a separate software. So my app shows when pressing space, and it hides when pressing space again.
Sergey Alexandrovich Kryukov 10-Apr-15 19:38pm    
Please see my other comment on this...
—SA
ChrisCreateBoss 10-Apr-15 19:53pm    
See my edit(in the question)
Sergey Alexandrovich Kryukov 10-Apr-15 20:10pm    
All right, this is all Mac OS X stuff; and the picture does not show much. How you do this Mac OS X and how is that related to C#? Is it Mono, or what?
—SA
ChrisCreateBoss 11-Apr-15 14:45pm    
No. It is C#. I'm just trying to make a "windows version" of that Mac OS X "preview system(called Quick Look)". Now do you understand what I mean? Also, the picture is showing the window that pops up if you press space when having selected a file in the Mac OS X Finder. As I said above, I'm trying to imitate that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900