Esempio n. 1
0
        internal static int Add(KeyModifier modifiers, Keys key, Action callback)
        {
            if (window == null)
            {
                window = new NativeHot();
            }

            int id = new Random().Next();

            callbacks.Add(id, callback);

            RegisterHotKey(window.Handle, id, (int)modifiers, (int)key);

            return(id);
        }
Esempio n. 2
0
        public KeydeemAppContext()
        {
            Program.Context = this;

            items["Exit"]        = new MenuItem("Exit", Exit);
            items["Keybindings"] = new MenuItem("Adjust keybindings...", AdjKeybindings);

            icon = new NotifyIcon()
            {
                Icon        = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location),
                ContextMenu = new ContextMenu(items.Values.ToArray()),
                Text        = "Keydeem - Logging in to Steam...",
                Visible     = true
            };

            icon.DoubleClick += (a, b) => {
                if (this.window != null)
                {
                    this.window.Activate();
                }
                else
                {
                    this.window = new MainWindow(log);
                    window.Show();

                    window.FormClosed += (c, d) => window = null;
                }
            };

            keybindings = Keybindings.Get(new Dictionary <string, Keybinding> {
                { "Redeem Clipboard", new Keybinding(KeyModifier.Super | KeyModifier.Shift, Keys.R) },
                { "Force Redeem Clipboard", new Keybinding(KeyModifier.Super | KeyModifier.Shift | KeyModifier.Control, Keys.R) }
            });

            steam = new Steam(successfulLogon => {
                if (!successfulLogon)
                {
                    Exit(null, null);
                    return;
                }

                icon.Text = "Keydeem";

                icon.ShowBalloonTip(0, "Logged on to Steam", "Press " + keybindings.bindings["Redeem Clipboard"] + " to redeem keys on your clipboard.", ToolTipIcon.Info);
            }, (detail, item) => {
                if (item == null)
                {
                    Program.Context.icon.ShowBalloonTip(0, "Key redemption failed", "" + detail, ToolTipIcon.Error);
                }
                else
                {
                    Program.Context.icon.ShowBalloonTip(0, "Key redemption successful", "You redeemed " + item, ToolTipIcon.Info);
                }
            });

            NativeHot.Add(keybindings.bindings["Redeem Clipboard"].GetModifiers(), keybindings.bindings["Redeem Clipboard"].Key, () => {
                if (!steam.loggedOn)
                {
                    icon.ShowBalloonTip(0, "Not logged on to Steam", "Log on to Steam before redeeming keys.", ToolTipIcon.Error);
                }
                else if (!Clipboard.ContainsText())
                {
                    icon.ShowBalloonTip(0, "Clipboard does not contain text", "Copy your key and try again.", ToolTipIcon.Error);
                }
                else
                {
                    MatchCollection matches = new Regex("(\\w{5}\\-\\w{5}\\-\\w{5})").Matches(Clipboard.GetText());

                    if (matches.Count == 0)
                    {
                        icon.ShowBalloonTip(0, "Clipboard does not contain standard Steam keys", "Keydeem only supports standard 5-5-5 Steam keys. To force redemption, use " + keybindings.bindings["Force Redeem Clipboard"] + ".", ToolTipIcon.Error);
                    }

                    foreach (Match match in matches)
                    {
                        steam.RedeemKey(match.Value);
                    }
                }
            });

            // I don't know why this is necessary
            Thread.Sleep(100);

            NativeHot.Add(keybindings.bindings["Force Redeem Clipboard"].GetModifiers(), keybindings.bindings["Force Redeem Clipboard"].Key, () => {
                if (!steam.loggedOn)
                {
                    icon.ShowBalloonTip(0, "Not logged on to Steam", "Log on to Steam before redeeming keys.", ToolTipIcon.Error);
                }
                else if (!Clipboard.ContainsText())
                {
                    icon.ShowBalloonTip(0, "Clipboard does not contain text", "Copy your key and try again.", ToolTipIcon.Error);
                }
                else
                {
                    steam.RedeemKey(Clipboard.GetText());
                }
            });
        }