如何在具有已创建功能的matlab中使用KeyPressFCN? [英] How to use KeyPressFCN in matlab with a function already create?

查看:415
本文介绍了如何在具有已创建功能的matlab中使用KeyPressFCN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求信息. 我和其他像我一样的学生必须在Matlab中创建声音.我们创建了它们,还必须创建一个interactif界面来播放这些声音.

i'm in seek of information. Me and a other students like me have to create sound in Matlab. We create them, and we have to create also an interactif interface to play those sound.

因此,我们创建了钢琴,然后单击按键,它会播放声音(即功能).

So we create a piano, and when we click on a key, it's play the sound ( that is the function. )

我们还希望我们可以在键盘上按一个调用该功能的键.我们听说过KeyPressFCN,但是我们不知道如何使用它,因为当我们搜索每个教程时,他们没有提供足够的信息.

We also wanted that we can push a key on the Keyboard that call the function. We heard about KeyPressFCN, but we don't know how to use it, because when we search every tutorial, they didn't give enough information about it.

因此,当我们右键单击所需的元素并将它们称为KeyPressFCN时,下一步是什么?我们必须要做些什么才能在此KeyPressFCN上放置"功能.

So, when we rightclick on the element we want, and them we call KeyPressFCN, what is the next step ? What did we have to do to "put" the function on this KeyPressFCN.

例如,要发出一种声音,我们有:

For example, to make one of the sound, we have :

% --- Execution lors d'un appui sur le bouton Do (première blanche)
function pushbutton1_Callback(hObject, eventdata, handles)
octave = str2double(get(handles.zone1,'String'));
frequence = 2093; %--- Fréquence initialement Do6
frequence2 = frequence./ octave;
son = sin(2*pi*frequence2*(0:0.000125:0.2));
sound(son);

推荐答案

实际上,我只是引用Matlab文档和帮助.

Actually I am just quoting Matlab docs and help.

  1. 如果使用的是GUIDE,请右键单击图形(而不是任何对象)>>查看回调>> KeyPressFcn,它将自动生成以下功能:

  1. If you are using GUIDE right click on your figure (not on any object) >> View Callbacks >> KeyPressFcn, then it will auto-generate the following function:

function figure1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  structure with the following fields (see FIGURE)
%   Key: name of the key that was pressed, in lower case
%   Character: character interpretation of the key(s) that was pressed
%   Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

% add this part as an experiment and see what happens!
eventdata % Let's see the KeyPress event data
disp(eventdata.Key) % Let's display the key, for fun!

用键盘弹奏并查看事件数据.显然,键入时该数字必须处于活动状态.

Play around with your keyboard and see the eventdata. Obviously the figure must be active when you are typing.

如果您使用的是uicontrol(而不是GUIDE),这是制作gui的编程方式

If you are using the uicontrol (and not GUIDE) which is the programmatic way of making gui

(使用内联函数)

fig_h = figure; % Open the figure and put the figure handle in fig_h
set(fig_h,'KeyPressFcn',@(fig_obj,eventDat) disp(['You just pressed: ' eventDat.Key])); 
% or again use the whole eventDat.Character or eventDat.Modifier if you want.

  • 或者如果您不想使用内联函数:

  • Or if you do not want to use inline function:

    fig_h = figure;
    set(fig_h,'KeyPressFcn', @key_pressed_fcn);
    

    然后像下面这样定义您的key_pressed_fcn:(创建一个新的mfile,名称为:key_pressed_fcn.m,当然,您可以使用任何想要的名称,但与上面的KeyPressFcn名称相同)

    and then define your key_pressed_fcn like: (create a new mfile with name: key_pressed_fcn.m, of course you could use whatever name you want but the same as KeyPressFcn name above)

    function key_pressed_fcn(fig_obj,eventDat)
    
    get(fig_obj, 'CurrentKey')
    get(fig_obj, 'CurrentCharacter')
    get(fig_obj, 'CurrentModifier')
    
    % or 
    
    disp(eventDat)
    

  • 或!使用脚本作为KeyPressFcn回调函数

  • OR! use a script as your KeyPressFcn callback function

    fig_h = figure;
    set(fig_h,'KeyPressFcn', 'key_pressed');
    

    然后编写key_pressed脚本:

    and then write key_pressed script:

    get(fig_h, 'CurrentKey')
    get(fig_h, 'CurrentCharacter')
    get(fig_h, 'CurrentModifier')
    

  • 有关Matlab的帮助,请参见以下文章中的"KeyPressFcn事件结构": http://www.mathworks.com/help/matlab/ref/figure_props.html

    For Matlab help refer to "KeyPressFcn Event Structure" in: http://www.mathworks.com/help/matlab/ref/figure_props.html

    这篇关于如何在具有已创建功能的matlab中使用KeyPressFCN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆