PDA

View Full Version : Shortcut override ?



fenerista
23rd May 2010, 09:50
http://www.uploadgeek.com/share-DF3C_4BF8C763.html

at a program like on the image, I want to make shorcut to skip forward or skip backward the video when I typing on the textEditor The key combinations I want : CTRL+Key_Left CTRL+Key_Right But these shortcuts are default shortcut on text editors. You can try when writing the post on this forum when you push CTRL+Key_Left it will go to the previous word, whatever. I want to deactive this shortcut on my texteditor and make the shortcuts to skip forward or backward for player. The shorcuts must be active when I typing on text editor and they must skip the video backward or forward.

tbscope
23rd May 2010, 09:57
You can use QShortcut.
But how do you now you're not in the text edit? By focus?
How will that work? How will you set the focus on another object?

Personally, I do not think this is a good idea.
I prefer using a shortcut that isn't already used.

fenerista
23rd May 2010, 10:33
I already want that shortcuts work/active when I am in texteditor when typing! I do the shortcuts as ALT+A AND ALT+D when writing when I press ALT+A it works but when I do the shortcuts as CTRL+Key_Left and CTRL_Right it doesn't work it just goes to the next work or previous words( ctrl+key_left and ctrl+key_right are the shortcuts for these actions on any editor) the problem is I can't override them This a project and my theacher exactly wants CTRL+Key_Left and CTRL_Right to conrol the player it's ridicilious But I must do that.

the text edit is the mainwindow and the player is created in it like


TextEdit::TextEdit(QWidget *parent)
: QMainWindow(parent)
{...

MediaPlayer *player= new MediaPlayer(fileString, hasSmallScreen);
QWidget* player2 =qobject_cast<MediaPlayer *>(player);
QSplitter *splitter = new QSplitter(Qt::Horizontal);
splitter->addWidget(player2);
splitter->addWidget(textEdit);

...

}

I try this


TextEdit::TextEdit(QWidget *parent)
: QMainWindow(parent){
...
shortcuts[SH_SLIDER_DOWN] = new QShortcut(this);
shortcuts[SH_SLIDER_DOWN]->setKey(QKeySequence(Qt::CTRL+Qt::Key_Left));
shortcuts[SH_SLIDER_UP] = new QShortcut(this);
shortcuts[SH_SLIDER_UP]->setKey(QKeySequence(Qt::ALT+Qt::Key_D));
shortcuts[SH_PLAYORPAUSE] = new QShortcut(this);
shortcuts[SH_PLAYORPAUSE]->setKey(QKeySequence(Qt::ALT+Qt::Key_W));

connect(shortcuts[SH_SLIDER_UP], SIGNAL(activated()), player, SLOT(stepForward()));
connect(shortcuts[SH_SLIDER_DOWN], SIGNAL(activatedAmbiguously()), player, SLOT(stepBackward()));
// I tried this too but nothing. connect(shortcuts[SH_SLIDER_DOWN], SIGNAL(activated()), player, SLOT(stepBackward()));
connect(shortcuts[SH_PLAYORPAUSE], SIGNAL(activated()), player, SLOT(playPause()));
...
}


But the CTRL+Key_Left just make the cursor posion previous word. I cant't override it.CTRL+Key_Left I think is a windows shortcut I don't know, it works in every where WORD, email or on this post :)

tbscope
23rd May 2010, 10:45
Subclass QTextEdit and reimplement void QTextEdit::keyPressEvent(QKeyEvent *e)
Just pass all the key events except the ones you need for other purposes.

fenerista
23rd May 2010, 10:54
ok
TextEdit is created like this class TextEdit : public QMainWindow. It contains QTextEdit class(and have this on TextEdit.h, QTextEdit *textEdit;). You say this ? make a new class that inherits from Qtextedit and change its keypresseventfuntion.

and change this QTextEdit *textEdit to QTextEdit *mytextEdit and do other changes on TextEdit constructor).


But I don't know how I searched this and I tried it but I couldn't. Just I know I should call the orijinal funtion at the last of my keypressevet function.

How cant I get ctrl+key_left


void CodeEditor::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Left: //1
if (event->modifiers() & Qt::ControlModifier) {//2 , 1 and 2. line is it enougfor CTRL+ Key_Left ? or it doen't work for that I want.
goToBeginningOfDocument();
} else {
goToBeginningOfLine();
}
break;
case Qt::Key_End:
...
default:
QWidget::keyPressEvent(event);
}
}

I know it's for codeeditor but I can make the changes for textedit.

tbscope
23rd May 2010, 11:38
For the key sequences, check out http://doc.qt.nokia.com/4.6/qkeysequence.html

fenerista
23rd May 2010, 12:02
#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H

#include<QtGui>

class myTextEdit : public QTextEdit{
Q_OBJECT
public:

myTextEdit(QWidget *parent=0): QTextEdit(parent)
{
}

void keyPressEvent(QKeyEvent* e)
{
if(e->key()== Qt::Key_Left){
if(e->modifiers()& Qt::ControlModifier)
emit PushedCtrlL();
else
QTextEdit::keyPressEvent(e);
}
else
QTextEdit::keyPressEvent(e);
}

signals:
void PushedCtrlL(void);
};

#endif // MYTEXTEDIT_H


I did myText edit
I overrided the keypressevent function
and send a signal when CTRL+LEFT pushed


TextEdit::TextEdit(QWidget *parent)
: QMainWindow(parent)
{
...


connect(textEdit, SIGNAL(PushedCtrlL(void)),player, SLOT(stepBackward()));
}

and it worked thx QT for signal and slot mechanism :)

Just a little question


if(e->key()== Qt::Key_Left){
if(e->modifiers()& Qt::ControlModifier)
is there any better way for those two lines
I don't know like

if(e->key()==qkeysequence("CTRL+Keyleft")){
emit PushedCtrlL();}


I am asking for that if I want ctrl+V+key_left for the stepbackward so what the two lines should be?