예제 #1
0
 public void JogCancel()
 {
     while (Comms.com.OutCount != 0)
     {
         ;
     }
     Comms.com.WriteByte(GrblConstants.CMD_JOG_CANCEL); // Cancel jog
     jogMode = JogMode.None;
 }
예제 #2
0
 public void JogCancel()
 {
     this.streamingState = StreamingState.Idle;
     while (Comms.com.OutCount != 0)
     {
         Application.DoEvents();                              //??
     }
     Comms.com.WriteByte((byte)GrblConstants.CMD_JOG_CANCEL); // Cancel jog
     this.jogMode = JogMode.None;
 }
예제 #3
0
        public bool ProcessKeypress(KeyEventArgs e, bool allowJog, UserControl context = null)
        {
            bool isJogging = IsJogging, jogkeyPressed = false;

            double[] dist = new double[4] {
                0d, 0d, 0d, 0d
            };

            if (e.IsUp && isJogging)
            {
                bool cancel = !allowJog;

                isJogging = false;

                for (int i = 0; i < N_AXIS; i++)
                {
                    if (axisjog[i] == e.Key)
                    {
                        axisjog[i] = Key.None;
                        cancel     = true;
                    }
                    else
                    {
                        isJogging = isJogging || (axisjog[i] != Key.None);
                    }
                }

                isJogging &= allowJog;

                if (cancel && !isJogging && jogMode != JogMode.Step)
                {
                    JogCancel();
                }
            }

            if (!isJogging && allowJog && Comms.com.OutCount != 0)
            {
                return(true);
            }

            this.allowJog = allowJog;

            if (IsJoggingEnabled && e.IsDown && CanJog)
            {
                // Do not respond to autorepeats!
                if (e.IsRepeat)
                {
                    return(true);
                }

                N_AXIS = GrblInfo.AxisFlags.HasFlag(AxisFlags.A) ? 4 : 3;

                switch (e.Key)
                {
                case Key.PageUp:
                    isJogging = axisjog[GrblConstants.Z_AXIS] != Key.PageUp;
                    axisjog[GrblConstants.Z_AXIS] = Key.PageUp;
                    break;

                case Key.PageDown:
                    isJogging = axisjog[GrblConstants.Z_AXIS] != Key.PageDown;
                    axisjog[GrblConstants.Z_AXIS] = Key.PageDown;
                    break;

                case Key.Left:
                    isJogging = axisjog[GrblConstants.X_AXIS] != Key.Left;
                    axisjog[GrblConstants.X_AXIS] = Key.Left;
                    break;

                case Key.Up:
                    isJogging = axisjog[GrblConstants.Y_AXIS] != Key.Up;
                    axisjog[GrblConstants.Y_AXIS] = Key.Up;
                    break;

                case Key.Right:
                    isJogging = axisjog[GrblConstants.X_AXIS] != Key.Right;
                    axisjog[GrblConstants.X_AXIS] = Key.Right;
                    break;

                case Key.Down:
                    isJogging = axisjog[GrblConstants.Y_AXIS] != Key.Down;
                    axisjog[GrblConstants.Y_AXIS] = Key.Down;
                    break;

                case Key.Home:
                    if (N_AXIS == 4)
                    {
                        isJogging = axisjog[GrblConstants.A_AXIS] != Key.Home;
                        axisjog[GrblConstants.A_AXIS] = Key.Home;
                    }
                    break;

                case Key.End:
                    if (N_AXIS == 4)
                    {
                        isJogging = axisjog[GrblConstants.A_AXIS] != Key.End;
                        axisjog[GrblConstants.A_AXIS] = Key.End;
                    }
                    break;
                }
            }
            else
            {
                jogkeyPressed = !(Keyboard.FocusedElement is System.Windows.Controls.TextBox) && (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.PageUp || e.Key == Key.PageDown);
            }

            if (isJogging)
            {
                string command = string.Empty;

                if (GrblInfo.LatheModeEnabled)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.Left:
                            dist[GrblConstants.Z_AXIS] = -1d;
                            command += "Z-{3}";
                            break;

                        case Key.Up:
                            dist[GrblConstants.X_AXIS] = -1d;
                            command += "X-{1}";
                            break;

                        case Key.Right:
                            dist[GrblConstants.Z_AXIS] = 1d;
                            command += "Z{3}";
                            break;

                        case Key.Down:
                            dist[GrblConstants.X_AXIS] = 1d;
                            command += "X{1}";
                            break;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < N_AXIS; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.PageUp:
                            dist[GrblConstants.Z_AXIS] = 1d;
                            command += "Z{3}";
                            break;

                        case Key.PageDown:
                            dist[GrblConstants.Z_AXIS] = -1d;
                            command += "Z-{3}";
                            break;

                        case Key.Left:
                            dist[GrblConstants.X_AXIS] = -1d;
                            command += "X-{1}";
                            break;

                        case Key.Up:
                            dist[GrblConstants.Y_AXIS] = 1d;
                            command += "Y{2}";
                            break;

                        case Key.Right:
                            dist[GrblConstants.X_AXIS] = 1d;
                            command += "X{1}";
                            break;

                        case Key.Down:
                            dist[GrblConstants.Y_AXIS] = -1d;
                            command += "Y-{2}";
                            break;

                        case Key.Home:
                            dist[GrblConstants.A_AXIS] = 1d;
                            command += "A{4}";
                            break;

                        case Key.End:
                            dist[GrblConstants.A_AXIS] = -1d;
                            command += "A-{4}";
                            break;
                        }
                    }
                }

                if ((isJogging = command != string.Empty))
                {
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        for (int i = 0; i < N_AXIS; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        preCancel = !(jogMode == JogMode.Step || jogMode == JogMode.None);
                        jogMode   = JogMode.Step;
                        JogDistances[(int)jogMode] = grbl.JogStep;
                    }
                    else if (IsContinuousJoggingEnabled)
                    {
                        preCancel = true;
                        if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                        {
                            jogMode = JogMode.Fast;
                        }
                        else
                        {
                            jogMode = JogMode.Slow;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < N_AXIS; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        jogMode = JogMode.None;
                    }

                    if (jogMode != JogMode.None)
                    {
                        if (GrblInfo.IsGrblHAL || !SoftLimits)
                        {
                            var distance = JogDistances[(int)jogMode].ToInvariantString();
                            SendJogCommand("$J=G91G21" + string.Format(command + "F{0}",
                                                                       JogFeedrates[(int)jogMode].ToInvariantString(),
                                                                       distance, distance, distance, distance));
                        }
                        else
                        {
                            for (int i = 0; i < N_AXIS; i++)
                            {
                                if (dist[i] != 0d)
                                {
                                    dist[i] = grbl.MachinePosition.Values[i] + JogDistances[(int)jogMode] * dist[i];

                                    if (i == GrblConstants.A_AXIS && GrblInfo.MaxTravel.Values[GrblConstants.A_AXIS] == 0d)
                                    {
                                        continue;
                                    }

                                    if (GrblInfo.ForceSetOrigin)
                                    {
                                        if (!GrblInfo.HomingDirection.HasFlag(GrblInfo.AxisIndexToFlag(i)))
                                        {
                                            if (dist[i] > 0)
                                            {
                                                dist[i] = 0;
                                            }
                                            else if (dist[i] < (-GrblInfo.MaxTravel.Values[i] + LimitSwitchesClearance))
                                            {
                                                dist[i] = (-GrblInfo.MaxTravel.Values[i] + LimitSwitchesClearance);
                                            }
                                        }
                                        else
                                        {
                                            if (dist[i] < 0d)
                                            {
                                                dist[i] = 0d;
                                            }
                                            else if (dist[i] > (GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance))
                                            {
                                                dist[i] = GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (dist[i] > -LimitSwitchesClearance)
                                        {
                                            dist[i] = -LimitSwitchesClearance;
                                        }
                                        else if (dist[i] < -(GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance))
                                        {
                                            dist[i] = -(GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance);
                                        }
                                    }
                                }
                            }

                            SendJogCommand("$J=G53G21" + string.Format(command.Replace('-', ' ') + "F{0}",
                                                                       JogFeedrates[(int)jogMode].ToInvariantString(),
                                                                       dist[GrblConstants.X_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.Y_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.Z_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.A_AXIS].ToInvariantString()));
                        }
                    }

                    return(jogMode != JogMode.None);
                }
            }

            IsRepeating = e.IsRepeat;

            if (Keyboard.Modifiers == ModifierKeys.Alt)
            {
                var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.SystemKey && k.onUp == e.IsUp && k.context == context).FirstOrDefault();
                if (handler != null)
                {
                    return(handler.Call(e.SystemKey));
                }
                else
                {
                    handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.SystemKey && k.onUp == e.IsUp && k.context == null).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.SystemKey));
                    }
                }
            }
            else if (Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Control || Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
            {
                var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.Key && k.onUp == e.IsUp && k.context == context).FirstOrDefault();
                if (handler != null)
                {
                    return(handler.Call(e.Key));
                }
                else
                {
                    handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.Key && k.onUp == e.IsUp && k.context == null).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.Key));
                    }
                }
            }

            return(jogkeyPressed);
        }
예제 #4
0
        public bool ProcessKeypress(ref Message msg)
        {
            Keys   keycode = (Keys)msg.WParam.ToInt32();
            string command = "";

            if (keycode == Keys.Space && this.grblState.State != GrblStates.Idle)
            {
                SendRTCommand(GrblConstants.CMD_FEED_HOLD);
                return(true);
            }

            if (Control.ModifierKeys == Keys.Alt)
            {
                if (keycode == Keys.S)
                {
                    SetStreamingState(StreamingState.Stop);
                    return(true);
                }

                if (keycode == Keys.R)
                {
                    this.CycleStart();
                    return(true);
                }
            }

            bool isJogging = this.jogMode != JogMode.None;

            if (msg.Msg == WM_KEYUP && (isJogging || this.grblState.State == GrblStates.Jog))
            {
                bool cancel = false;

                isJogging = false;

                for (int i = 0; i < 3; i++)
                {
                    if (axisjog[i] == keycode)
                    {
                        axisjog[i] = Keys.None;
                        cancel     = true;
                    }
                    else
                    {
                        isJogging = isJogging || (axisjog[i] != Keys.None);
                    }
                }

                if (cancel && !isJogging && this.jogMode != JogMode.Step)
                {
                    this.JogCancel();
                }
            }

            if (!isJogging && Comms.com.OutCount != 0)
            {
                return(true);
            }

//            if ((keycode == Keys.ShiftKey || keycode == Keys.ControlKey) && !this.isJogging)
//                return false;

            if (msg.Msg == WM_KEYDOWN && this.canJog)
            {
                // Do not respond to autorepeats!
                if ((msg.LParam.ToInt32() & (1 << 30)) != 0)
                {
                    return(true);
                }

                switch (keycode)
                {
                case Keys.PageUp:
                    isJogging  = axisjog[2] != Keys.PageUp;
                    axisjog[2] = Keys.PageUp;
                    break;

                case Keys.PageDown:
                    isJogging  = axisjog[2] != Keys.PageDown;
                    axisjog[2] = Keys.PageDown;
                    break;

                case Keys.Left:
                    isJogging  = axisjog[0] != Keys.Left;
                    axisjog[0] = Keys.Left;
                    break;

                case Keys.Up:
                    isJogging  = axisjog[1] != Keys.Up;
                    axisjog[1] = Keys.Up;
                    break;

                case Keys.Right:
                    isJogging  = axisjog[0] != Keys.Right;
                    axisjog[0] = Keys.Right;
                    break;

                case Keys.Down:
                    isJogging  = axisjog[1] != Keys.Down;
                    axisjog[1] = Keys.Down;
                    break;
                }
            }

            if (isJogging)
            {
                if (GrblInfo.LatheMode)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Keys.Left:
                            command += "Z-{0}";
                            break;

                        case Keys.Up:
                            command += "X-{0}";
                            break;

                        case Keys.Right:
                            command += "Z{0}";
                            break;

                        case Keys.Down:
                            command += "X{0}";
                            break;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Keys.PageUp:
                            command += "Z{0}";
                            break;

                        case Keys.PageDown:
                            command += "Z-{0}";
                            break;

                        case Keys.Left:
                            command += "X-{0}";
                            break;

                        case Keys.Up:
                            command += "Y{0}";
                            break;

                        case Keys.Right:
                            command += "X{0}";
                            break;

                        case Keys.Down:
                            command += "Y-{0}";
                            break;
                        }
                    }
                }

                if ((isJogging = command != ""))
                {
                    if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
                    {
                        this.jogMode = JogMode.Step;
                    }
                    else if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                    {
                        this.jogMode = JogMode.Fast;
                    }
                    else
                    {
                        this.jogMode = JogMode.Slow;
                    }

                    this.SendJogCommand("$J=G91" + string.Format(command + "F{1}",
                                                                 jogDistance[(int)this.jogMode].ToString(CultureInfo.InvariantCulture),
                                                                 jogSpeed[(int)this.jogMode].ToString(CultureInfo.InvariantCulture)));
                }
            }

            return(isJogging);
        }
예제 #5
0
        public bool ProcessKeypress(KeyEventArgs e)
        {
            string command = "";

            if (e.Key == Key.Space && grblState.State != GrblStates.Idle)
            {
                btnHold_Click(null, null);
                return(true);
            }

            if (Keyboard.Modifiers == ModifierKeys.Alt)
            {
                if (e.SystemKey == Key.S)
                {
                    streamingHandler.Call(StreamingState.Stop, false);
                    return(true);
                }

                if (e.SystemKey == Key.R)
                {
                    CycleStart();
                    return(true);
                }
            }

            bool isJogging = jogMode != JogMode.None;

            if (e.IsUp && (isJogging || grblState.State == GrblStates.Jog))
            {
                bool cancel = false;

                isJogging = false;

                for (int i = 0; i < 3; i++)
                {
                    if (axisjog[i] == e.Key)
                    {
                        axisjog[i] = Key.None;
                        cancel     = true;
                    }
                    else
                    {
                        isJogging = isJogging || (axisjog[i] != Key.None);
                    }
                }

                if (cancel && !isJogging && jogMode != JogMode.Step)
                {
                    JogCancel();
                }
            }

            if (!isJogging && Comms.com.OutCount != 0)
            {
                return(true);
            }

            //            if ((keycode == Keys.ShiftKey || keycode == Keys.ControlKey) && !isJogging)
            //                return false;

            if (e.IsDown && canJog)
            {
                // Do not respond to autorepeats!
                if (e.IsRepeat)
                {
                    return(true);
                }

                switch (e.Key)
                {
                case Key.PageUp:
                    isJogging  = axisjog[2] != Key.PageUp;
                    axisjog[2] = Key.PageUp;
                    break;

                case Key.PageDown:
                    isJogging  = axisjog[2] != Key.PageDown;
                    axisjog[2] = Key.PageDown;
                    break;

                case Key.Left:
                    isJogging  = axisjog[0] != Key.Left;
                    axisjog[0] = Key.Left;
                    break;

                case Key.Up:
                    isJogging  = axisjog[1] != Key.Up;
                    axisjog[1] = Key.Up;
                    break;

                case Key.Right:
                    isJogging  = axisjog[0] != Key.Right;
                    axisjog[0] = Key.Right;
                    break;

                case Key.Down:
                    isJogging  = axisjog[1] != Key.Down;
                    axisjog[1] = Key.Down;
                    break;
                }
            }

            if (isJogging)
            {
                if (GrblInfo.LatheModeEnabled)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.Left:
                            command += "Z-{0}";
                            break;

                        case Key.Up:
                            command += "X-{0}";
                            break;

                        case Key.Right:
                            command += "Z{0}";
                            break;

                        case Key.Down:
                            command += "X{0}";
                            break;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.PageUp:
                            command += "Z{0}";
                            break;

                        case Key.PageDown:
                            command += "Z-{0}";
                            break;

                        case Key.Left:
                            command += "X-{0}";
                            break;

                        case Key.Up:
                            command += "Y{0}";
                            break;

                        case Key.Right:
                            command += "X{0}";
                            break;

                        case Key.Down:
                            command += "Y-{0}";
                            break;
                        }
                    }
                }

                if ((isJogging = command != ""))
                {
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        jogMode = JogMode.Step;
                    }
                    else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                    {
                        jogMode = JogMode.Fast;
                    }
                    else
                    {
                        jogMode = JogMode.Slow;
                    }

                    SendJogCommand("$J=G91" + string.Format(command + "F{1}",
                                                            jogDistance[(int)jogMode].ToInvariantString(),
                                                            jogSpeed[(int)jogMode].ToInvariantString()));
                }
            }

            return(isJogging);
        }
예제 #6
0
        public bool ProcessKeypress(KeyEventArgs e)
        {
            bool isJogging = jogMode != JogMode.None;

            if (e.IsUp && (isJogging || grbl.GrblState.State == GrblStates.Jog))
            {
                bool cancel = false;

                isJogging = false;

                for (int i = 0; i < 3; i++)
                {
                    if (axisjog[i] == e.Key)
                    {
                        axisjog[i] = Key.None;
                        cancel     = true;
                    }
                    else
                    {
                        isJogging = isJogging || (axisjog[i] != Key.None);
                    }
                }

                if (cancel && !isJogging && jogMode != JogMode.Step)
                {
                    JogCancel();
                }
            }

            if (!isJogging && Comms.com.OutCount != 0)
            {
                return(true);
            }

            //            if ((keycode == Keys.ShiftKey || keycode == Keys.ControlKey) && !isJogging)
            //                return false;

            if (e.IsDown && canJog)
            {
                // Do not respond to autorepeats!
                if (e.IsRepeat)
                {
                    return(true);
                }

                switch (e.Key)
                {
                case Key.PageUp:
                    isJogging  = axisjog[2] != Key.PageUp;
                    axisjog[2] = Key.PageUp;
                    break;

                case Key.PageDown:
                    isJogging  = axisjog[2] != Key.PageDown;
                    axisjog[2] = Key.PageDown;
                    break;

                case Key.Left:
                    isJogging  = axisjog[0] != Key.Left;
                    axisjog[0] = Key.Left;
                    break;

                case Key.Up:
                    isJogging  = axisjog[1] != Key.Up;
                    axisjog[1] = Key.Up;
                    break;

                case Key.Right:
                    isJogging  = axisjog[0] != Key.Right;
                    axisjog[0] = Key.Right;
                    break;

                case Key.Down:
                    isJogging  = axisjog[1] != Key.Down;
                    axisjog[1] = Key.Down;
                    break;
                }
            }

            if (isJogging)
            {
                string command = string.Empty;

                if (GrblInfo.LatheModeEnabled)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.Left:
                            command += "Z-{0}";
                            break;

                        case Key.Up:
                            command += "X-{0}";
                            break;

                        case Key.Right:
                            command += "Z{0}";
                            break;

                        case Key.Down:
                            command += "X{0}";
                            break;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.PageUp:
                            command += "Z{0}";
                            break;

                        case Key.PageDown:
                            command += "Z-{0}";
                            break;

                        case Key.Left:
                            command += "X-{0}";
                            break;

                        case Key.Up:
                            command += "Y{0}";
                            break;

                        case Key.Right:
                            command += "X{0}";
                            break;

                        case Key.Down:
                            command += "Y-{0}";
                            break;
                        }
                    }
                }

                if ((isJogging = command != string.Empty))
                {
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        preCancel = !(jogMode == JogMode.Step || jogMode == JogMode.None);
                        jogMode   = JogMode.Step;
                        jogDistance[(int)jogMode] = grbl.JogStep;
                    }
                    else if (fullJog)
                    {
                        preCancel = true;
                        if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                        {
                            jogMode = JogMode.Fast;
                        }
                        else
                        {
                            jogMode = JogMode.Slow;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        jogMode = JogMode.None;
                    }

                    if (jogMode != JogMode.None)
                    {
                        SendJogCommand("$J=G91" + string.Format(command + "F{1}",
                                                                jogDistance[(int)jogMode].ToInvariantString(),
                                                                jogSpeed[(int)jogMode].ToInvariantString()));
                    }

                    return(jogMode != JogMode.None);
                }
            }

            if (e.IsUp)
            {
                if (Keyboard.Modifiers == ModifierKeys.Alt)
                {
                    var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.SystemKey).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.SystemKey));
                    }
                }
                else if (Keyboard.Modifiers == ModifierKeys.None)
                {
                    var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.Key).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.Key));
                    }

                    else
                    {
                        switch (e.Key)
                        {
                        case Key.NumPad4:
                        {
                            var step = grbl.JogStep / 10d;
                            grbl.JogStep = grbl.IsMetric ? (step < 0.01d ? 0.01d : step) : (step < 0.001d ? 0.001d : step);
                        }
                            return(true);

                        //  break;

                        case Key.NumPad6:
                        {
                            var step = grbl.JogStep * 10d;
                            grbl.JogStep = grbl.IsMetric ? (step > 1d ? 1d : step) : (step > 0.1d ? 0.1d : step);
                        }
                            return(true);
                            //  break;
                        }
                    }
                }
            }

            return(false);
        }
예제 #7
0
        public bool ProcessKeypress(KeyEventArgs e, bool allowJog)
        {
            bool isJogging = IsJogging;

            double[] dist = new double[3] {
                0d, 0d, 0d
            };

            if (e.IsUp && isJogging)
            {
                bool cancel = !allowJog;

                isJogging = false;

                for (int i = 0; i < 3; i++)
                {
                    if (axisjog[i] == e.Key)
                    {
                        axisjog[i] = Key.None;
                        cancel     = true;
                    }
                    else
                    {
                        isJogging = isJogging || (axisjog[i] != Key.None);
                    }
                }

                isJogging &= allowJog;

                if (cancel && !isJogging && jogMode != JogMode.Step)
                {
                    JogCancel();
                }
            }

            if (!isJogging && allowJog && Comms.com.OutCount != 0)
            {
                return(true);
            }

            if (e.IsDown && CanJog && allowJog)
            {
                // Do not respond to autorepeats!
                if (e.IsRepeat)
                {
                    return(true);
                }

                switch (e.Key)
                {
                case Key.PageUp:
                    isJogging = axisjog[GrblConstants.Z_AXIS] != Key.PageUp;
                    axisjog[GrblConstants.Z_AXIS] = Key.PageUp;
                    break;

                case Key.PageDown:
                    isJogging = axisjog[GrblConstants.Z_AXIS] != Key.PageDown;
                    axisjog[GrblConstants.Z_AXIS] = Key.PageDown;
                    break;

                case Key.Left:
                    isJogging = axisjog[GrblConstants.X_AXIS] != Key.Left;
                    axisjog[GrblConstants.X_AXIS] = Key.Left;
                    break;

                case Key.Up:
                    isJogging = axisjog[GrblConstants.Y_AXIS] != Key.Up;
                    axisjog[GrblConstants.Y_AXIS] = Key.Up;
                    break;

                case Key.Right:
                    isJogging = axisjog[GrblConstants.X_AXIS] != Key.Right;
                    axisjog[GrblConstants.X_AXIS] = Key.Right;
                    break;

                case Key.Down:
                    isJogging = axisjog[GrblConstants.Y_AXIS] != Key.Down;
                    axisjog[GrblConstants.Y_AXIS] = Key.Down;
                    break;
                }
            }

            if (isJogging)
            {
                string command = string.Empty;

                if (GrblInfo.LatheModeEnabled)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.Left:
                            dist[GrblConstants.Z_AXIS] = -1d;
                            command += "Z-{3}";
                            break;

                        case Key.Up:
                            dist[GrblConstants.X_AXIS] = -1d;
                            command += "X-{1}";
                            break;

                        case Key.Right:
                            dist[GrblConstants.Z_AXIS] = 1d;
                            command += "Z{3}";
                            break;

                        case Key.Down:
                            dist[GrblConstants.X_AXIS] = 1d;
                            command += "X{1}";
                            break;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.PageUp:
                            dist[GrblConstants.Z_AXIS] = 1d;
                            command += "Z{3}";
                            break;

                        case Key.PageDown:
                            dist[GrblConstants.Z_AXIS] = -1d;
                            command += "Z-{3}";
                            break;

                        case Key.Left:
                            dist[GrblConstants.X_AXIS] = -1d;
                            command += "X-{1}";
                            break;

                        case Key.Up:
                            dist[GrblConstants.Y_AXIS] = 1d;
                            command += "Y{2}";
                            break;

                        case Key.Right:
                            dist[GrblConstants.X_AXIS] = 1d;
                            command += "X{1}";
                            break;

                        case Key.Down:
                            dist[GrblConstants.Y_AXIS] = -1d;
                            command += "Y-{2}";
                            break;
                        }
                    }
                }

                if ((isJogging = command != string.Empty))
                {
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        preCancel = !(jogMode == JogMode.Step || jogMode == JogMode.None);
                        jogMode   = JogMode.Step;
                        jogDistance[(int)jogMode] = grbl.JogStep;
                    }
                    else if (fullJog)
                    {
                        preCancel = true;
                        if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                        {
                            jogMode = JogMode.Fast;
                        }
                        else
                        {
                            jogMode = JogMode.Slow;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        jogMode = JogMode.None;
                    }

                    if (jogMode != JogMode.None)
                    {
                        if (GrblInfo.IsGrblHAL || !softLimits)
                        {
                            var distance = jogDistance[(int)jogMode].ToInvariantString();
                            SendJogCommand("$J=G91G21" + string.Format(command + "F{0}",
                                                                       jogSpeed[(int)jogMode].ToInvariantString(),
                                                                       distance, distance, distance));
                        }
                        else
                        {
                            for (int i = 0; i < 3; i++)
                            {
                                if (dist[i] != 0d)
                                {
                                    if (GrblInfo.HomingDirection.HasFlag(GrblInfo.AxisIndexToFlag(i)))
                                    {
                                        dist[i] = dist[i] < 0d ? grbl.MachinePosition.Values[i] : Math.Max(0d, GrblInfo.MaxTravel.Values[i] - grbl.MachinePosition.Values[i] - .5d);
                                    }
                                    else
                                    {
                                        dist[i] = dist[i] > 0d ? (-grbl.MachinePosition.Values[i] - .5d) : Math.Max(0d, GrblInfo.MaxTravel.Values[i] + grbl.MachinePosition.Values[i] - .5d);
                                    }
                                }
                            }
                            SendJogCommand("$J=G91G21" + string.Format(command + "F{0}",
                                                                       jogSpeed[(int)jogMode].ToInvariantString(),
                                                                       dist[GrblConstants.X_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.Y_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.Z_AXIS].ToInvariantString()));
                        }
                    }

                    return(jogMode != JogMode.None);
                }
            }

            if (e.IsUp)
            {
                if (Keyboard.Modifiers == ModifierKeys.Alt)
                {
                    var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.SystemKey).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.SystemKey));
                    }
                }
                else if (Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Control || Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
                {
                    var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.Key).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.Key));
                    }

                    else
                    {
                        switch (e.Key)
                        {
                        case Key.NumPad4:
                            JogControl.JogData.StepDec();
                            return(true);

                        //  break;

                        case Key.NumPad6:
                            JogControl.JogData.StepInc();
                            return(true);

                        case Key.NumPad8:
                            JogControl.JogData.FeedInc();
                            return(true);

                        case Key.NumPad2:
                            JogControl.JogData.FeedDec();
                            return(true);
                            //  break;
                        }
                    }
                }
            }

            return(false);
        }